-3

I am dealing with around 9000 data in my excel dataset. My goal is to find the match value between A column (sheet1) and A column(sheet2) if there is a match then copy the whole row from sheet 2 and put beside match value in sheet1. This is the code I have if you guys have any suggestions to make it work faster then please do let me know.

Dim sht11 As Worksheet, sht22 As Worksheet

Set sht11 = Worksheets("sheet1")
Set sht22 = Worksheets("sheet2")

Sheet1LastRow = Worksheets("sheet1").Range("A" & 
 Rows.Count).End(xlUp).Row
Sheet2LastRow = Worksheets("sheet2").Range("A" & Rows.Count).End(xlUp).Row

For j = 1 To Sheet1LastRow

    For i = 1 To Sheet2LastRow

        If sht11.Cells(j, 1).Value = sht22.Cells(i, 1).Value Then
            sht11.Cells(j, 9).Resize(1, 124).Value = _
                          sht22.Cells(i, 9).Resize(1, 124).Value


        Else

        End If

      Next i

   Next j
Damian
  • 5,152
  • 1
  • 10
  • 21
GetSome _
  • 59
  • 8
  • 8
    This question is better suited for [code review](https://codereview.stackexchange.com/questions/tagged/vba) if your code works, they can help you. – Damian Jun 03 '19 at 07:33
  • Similar: https://stackoverflow.com/questions/55493268/fastest-way-to-transfer-large-amounts-of-data-between-worksheets – Dean Jun 03 '19 at 07:52
  • Also: https://stackoverflow.com/questions/33302962/performance-difference-between-looping-range-vs-looping-array – Dean Jun 03 '19 at 08:49

2 Answers2

0

I think this may help you:

Option Explicit

Sub test()

    Dim rngToSearchIn As Range, rngFound As Range
    Dim LastRow1 As Long, LastRow2 As Long, i As Long, LastColumn1 As Long, LastColumn2 As Long
    Dim arr As Variant
    Dim strSearchValue As String
    Dim ws1 As Worksheet, ws2 As Worksheet

    With ThisWorkbook
        Set ws1 = .Worksheets("Sheet1")
        Set ws2 = .Worksheets("Sheet2")
    End With

    With ws1
        LastRow1 = .Cells(.Rows.Count, "A").End(xlUp).Row
        arr = .Range("A1:A" & LastRow1)
    End With

    With ws2
        LastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rngToSearchIn = .Range("A1:A" & LastRow2)
    End With

    For i = LBound(arr) To UBound(arr)

        strSearchValue = arr(i, 1)

        Set rngFound = rngToSearchIn.Find(What:=strSearchValue, LookAt:=xlWhole, MatchCase:=True, SearchFormat:=False)

        If Not rngFound Is Nothing Then

            With ws2
                LastColumn2 = .Cells(rngFound.Row, .Columns.Count).End(xlToLeft).Column
                .Range(.Cells(rngFound.Row, 1), .Cells(rngFound.Row, LastColumn2)).Copy
            End With

            With ws1
                LastColumn1 = .Cells(i, .Columns.Count).End(xlToLeft).Column
                .Cells(i, LastColumn1 + 1).PasteSpecial Paste:=xlPasteValues
            End With

        End If

    Next i

End Sub
Error 1004
  • 7,877
  • 3
  • 23
  • 46
  • ++ for using arrays, but not sure about using `find` and `copy/paste`. Any reason why not `if this = that` and `.value = .value`? – FAB Jun 03 '19 at 08:40
  • The OP ask to copy the whole row, that s why i use copy paste. If this is not the best approach i ll change it. Let' s wait for OP feedback. – Error 1004 Jun 03 '19 at 08:50
0

While there is already a valid answer, in terms of speed the less you interact with the sheets, the better. See below for an alternative, and comments in the code for more details:

Sub copyValues()

Dim wb As Workbook: Set wb = ActiveWorkbook

Dim wsSrc As Worksheet: Set wsSrc = wb.Worksheets("Sheet2")
With wsSrc
    Dim lRowSrc As Long: lRowSrc = .Cells(.Rows.Count, 1).End(xlUp).Row 'get last row in source data
    Dim lColSrc As Long: lColSrc = .Cells(1, .Columns.Count).End(xlToLeft).Column 'get last column in source data
    Dim arrSrc As Variant: arrSrc = .Range(.Cells(1, 1), .Cells(lRowSrc, lColSrc)) 'allocate the data to an array
End With

Dim wsDst As Worksheet: Set wsDst = wb.Worksheets("Sheet1")
With wsDst
    Dim lRowDst As Long: lRowDst = .Cells(.Rows.Count, 1).End(xlUp).Row 'get last row in destination data
    Dim lColDst As Long: lColDst = 8 '.Cells(1, .Columns.Count).End(xlToLeft).Column 'get last column in destination data - if no other data, can use the dynamic version, otherwise use the set value i guess
    Dim arrDst As Variant: arrDst = .Range(.Cells(1, 1), .Cells(lRowDst, lColSrc + lColDst)) '
End With

Dim Rd As Long, Rs As Long, C As Long

For Rd = LBound(arrDst) To UBound(arrDst) 'iterate through all rows in the destination data
    For Rs = LBound(arrSrc) To UBound(arrSrc) 'iterate through all rows in the source data
        If arrDst(Rd, 1) = arrSrc(Rs, 1) Then 'if there is a match
            For C = LBound(arrDst, 2) + lColDst To UBound(arrDst, 2) 'iterate through all columns in the source
                arrDst(Rd, C) = arrSrc(Rs, C - lColDst) 'allocate to the destination array
            Next C

'alternatively, can write the values directly back to the sheet (comment the C loop above and values allocation below the loops)
'            With wsDst
'                .Range(.Cells(Rd, 9), .Cells(Rd, lColSrc + lColDst)).Value = _
'                    wsSrc.Range(wsSrc.Cells(Rs, 1), wsSrc.Cells(Rs, lColSrc)).Value
'            End With

            Exit For
        End If
    Next Rs
Next Rd

With wsDst
    .Range(.Cells(1, 1), .Cells(lRowDst, lColSrc + lColDst)) = arrDst 'put the values back on the sheet
End With

End Sub
FAB
  • 2,505
  • 1
  • 10
  • 21
  • It would be better to use a dictionary storing the matches on sheet 2. So you only loop once and check the dictionary. – Damian Jun 03 '19 at 09:26
  • @Damian I'm a bit crap when it comes to dictionaries, but interested to see how this solution would look using them. – FAB Jun 03 '19 at 09:28
  • Truth is I wrote the answer after writting the comment to code review... But I had to reboot the computer and totally forgot about the code so it's lost... Dictionaries are Key / Item. So you loop through the sheet 2, add the value in column A as Key and it's row as item. Then you just need to check `Dict.Exists()` in sheet1 and doing `Dict(Value)`will return you the row to loop through and get the columns you need. – Damian Jun 03 '19 at 09:34