简体   繁体   中英

Copy rows from Target sheet to oter sheets based on cell values

I am having some difficulty with (vba lookup) issue.

I Have a sheet (sheet3) which has multiple rows of data of different invoices (each row of data includes the invoice number it relates to) Data sheet

I have copied the unique invoice numbers into separate sheets, each invoice has its own sheet and the invoice number is in cell B1. invoice sheet

What I want to do is to copy all rows from the data sheet to the sheet with the matching invoice number.

all I have for my current code is this which My separate invoice pages link of rather than using Vba to create them as there will be various other formatting and Formulrs on the page so im pretty much starting from scratch on my issue!

  Private Sub CommandButton1_Click()
     Dim s1 As Worksheet, s2 As Worksheet
     Set s1 = Sheets("sheet3")
     Set s2 = Sheets("Bill Date")
     s1.Range("F:G").Copy s2.Range("A:B")
     s2.Range("A:B").RemoveDuplicates Columns:=1, Header:=xlNo
  End Sub

Your help will be appreciated

Thanks

In your VBA Macro, do this within a for loop:

Sub copyData()
    Dim invNo As String
    Dim lastRow As Integer
    Dim sourceSht As Worksheet
    Dim targSht As Worksheet

    Set sourceSht = Worksheets("Sheet3")

    'evaluates every data item from row 2 to last populated row
    For Row = 2 To sourceSht.Cells(sourceSht.Rows.Count, 1).End(xlUp).Row

        invNo = sourceSht.Range("F" & Row).Value

        'if invNo blank, skip
        If invNo <> "" Then
            'try to find the sheet, make if does not exist
            invNo = invNo & "_INV"
            On Error Resume Next
            Set targSht = Worksheets(invNo)
            If targSht Is Nothing Then
                Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = invNo
                Set targSht = Worksheets(invNo)
                'SetHeader
            End If

            'find first empty row in targSht
            lastRow = targSht.Cells(targSht.Rows.Count, 1).End(xlUp).Row + 1

            'copy row of data
            sourceSht.Range("A" & Row & ":L" & Row).Copy
            targSht.Range("A" & lastRow & ":L" & lastRow).Select
            targSht.Paste

            'must do to make more sheets
            Set targSht = Nothing
        End If
    Next
End Sub

I changed some of your specifications in favor of a simpler approach. I assumed the twelve columns you showed me are all you have. I added "_INV" to the end of the invoice sheets because purely numeric sheet names can cause errors. I am also pasting the row of data into the new sheet verbatim. If you keep your current header, you will need to change the order. You may consider changing your targSht header to make it easier. SetHeader is a placeholder for a block of code that sets up the header row in targSht however you want. Please mark correct if this solves your issue.

Demo (without invoice header): 跑步前 运行后

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM