In Excel how do I copy only wanted cells with specific order with VBA

Tilen

My code copies the whole thing below last cell by routes now.

I want to filter not only by route but with shipped aswell - so with yes or no.

I also want to copy only selected cells from here to here. like images suggest by the following order (eg. cell C2 to cell A1 in another workbook).

my code looks like this:

Private Sub CommandButton1_Click()
Dim cell As Range

With Workbooks("FromHEre.xlsm").Worksheets("Sheet1")

For Each cell In .Range("D1:D" & .Cells(.Rows.Count, "D").End(xlUp).Row)
If cell.Value = "1" Then

.Rows(cell.Row).Copy Destination:=Workbooks("ToHere.xlsx").Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1)
End If
Next cell
End With
End Sub
Ricardo Diaz

This is by no means a complete answer and it's un-tested, but should get you started.

Read the comments and adapt the code to fit your needs

Private Sub CommandButton1_Click()
    Dim sourceWorkbook As Workbook
    Dim sourceSheet As Worksheet
    Dim sourceRange As Range
    Dim sourceFilteredRange As Range

    Dim targetWorkbook As Workbook
    Dim targetSheet As Worksheet
    Dim targetCell As Range

    Dim cell As Range

    Dim sourceLastRow As Long
    Dim targetLastRow As Long

    ' Define source and target objects
    Set sourceWorkbook = Workbooks("FromHere.xlsm")
    Set sourceSheet = sourceWorkbook.Worksheets("Sheet1")
    Set targetWorkbook = Workbooks("ToHere.xlsx")
    Set targetSheet = targetWorkbook.Worksheets("Sheet1")

    ' Get last row of source sheet
    sourceLastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row

    ' Get last row of target sheet
    targetLastRow = targetSheet.Cells(targetSheet.Rows.Count, "A").End(xlUp).Row

    ' Set source range
    Set sourceRange = sourceSheet.Range("A1:H" & sourceLastRow)

    ' Filter source range by route and shipped
    With sourceRange
        .AutoFilter Field:=4, Criteria1:="1"
        .AutoFilter Field:=7, Criteria1:="yes"
    End With

    ' Get filtered range
    Set sourceFilteredRange = sourceRange.AutoFilter.Range

    ' Copy filtered range to target sheet
    sourceFilteredRange.Copy targetSheet.Range("A" & targetLastRow)

End Sub

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I copy only specific cells

How do I copy specific selected data between cells in a CSV file in Excel or Notepad++?

In Excel how do I check that all cells in a range contain only a specific string

How to do a summation only within specific cells via VBA

How do I copy some specific columns from a given range which is auto filtered in Excel VBA?

vba loop in order to copy specific cells from one sheet to another

Excel VBA, how do i add value to a number of cells?

How do I to select and copy different parts of excel to excel in VBA?

How do I search with Excel for cells with specific characters at the end of the string?

How can i copy this cells as this order?

How do i Subtract only 2 cells in excel IF they are not blank

How do I copy one image to another using Excel VBA

How can I delete empty cells in Excel using VBA after a Copy/Paste?

How copy vba cells

How can I automatically copy data from one Excel sheet to another and have it sort into specific cells?

How do I copy cells value when the description cell has a specific word in cell

wanted to copy specific text from the text file and keep it in one temp variable how to do that

Excel VBA, How to copy cells from within a Column object

How to copy/paste formula in filtered/visible cells via VBA in Excel

How do I copy Word tables into Excel without splitting cells into multiple rows?

How to make excel automatically copy specific cells with date on its side

VBA to copy only specific columns in excel to export as csv

Excel VBA to copy only non blank rows from specific column

VBA Split Cells and paste only specific cells

How do I put borders on specific cells

How do I loop through cells and create a new row if it contains a specific string in vba?

How to protect a specific workbook only in Excel VBA

VBA - How do I compare text in Excel cells to see if same words are found, exclusing punctuation?

VBA excel: How do I get data in cells as an array up one row in the same column without selecting?