I solved it on my own. I added a for loop. Here is my working code. Thanks to everyone else for trying to help.
Sub runMatch()
Dim critRemID, listRemID, critRemIDstart, listRemIDstart As Range
Set critRemID = Worksheets("Enterprise - score").Cells(2, 1)
Set listRemID = Worksheets("Sheet1").Cells(2, 1)
Set critRemIDstart = Worksheets("Enterprise - score").Cells(2, 30)
Set listRemIDstart = Worksheets("Sheet1").Cells(2, 2)
Dim i, j, index As Integer
i = 0
j = 0
Do While critRemID.Offset(i, 0) <> ""
If critRemID.Offset(i, 0) = listRemID.Offset(j, 0) Then
For index = 0 To 84
critRemIDstart.Offset(i, index) = listRemIDstart.Offset(j, index).Value
Next index
i = i + 1
j = 0
index = 0
Else
If listRemID.Offset(j, 0) = "" Then
j = 0
i = i + 1
Else
j = j + 1
End If
End If
Loop
End Sub
I have two sheets, they each have a the same IDs on each sheet but different sets of data.
I want to scan through the rows of data and if there is a match, copy the entire row from a certain column to another certain column to the end of one of the sheets.
Sheet 1 is the sheet I want to copy info into, on the end I've created the same headers for the data I want to bring over from sheet 2.
the code below is what I have, I set a range up for the IDs and one for where I want the copied cells to start
Dim critRemID, listRemID, critRemIDstart, listRemIDstart As Range Set critRemID = Worksheets("Enterprise - score").Cells(2, 1) Set listRemID = Worksheets("Sheet1").Cells(2, 1) Set critRemIDstart = Worksheets("Enterprise - score").Cells(2, 30) Set listRemIDstart = Worksheets("Sheet1").Cells(2, 90) Dim i, j As Integer i = 0 j = 0 Do While critRemID.Offset(i, 0) <> "" If critRemID.Offset(i, 0) = listRemID.Offset(j, 0) Then critRemIDstart.Row(i) = listRemIDstart.Row(j).Value i = i + 1 j = 0 Else j = j + 1 End If Loop
I keep getting this error
Wrong number of arguments or invalid property assignment
I tried going a different route but kept getting confused as shown below. I was trying to have it copy each cell one by one and once it reached an empty cell, it would move onto the next ID on the main sheet and start over but this does nothing, I think it keeps increasing both IDs on the sheet and never finds a match.
If critRemID.Offset(i, 0) = listRemID.Offset(j, 0) Then critRemIDstart.Offset(i, k) = listRemIDstart.Offset(j, l).Value k = k + 1 l = l + 1 Else If listRemIDstart.Offset(j, l) = "" Then j = j + 1 l = 0 i = i + 1 k = 0 Else j = j + 1 i = i + 1 l = 0 k = 0 End If End if
any help is appreciated. Thanks.
Range.Find
method could find the key easily.
Dim critRem, listRem As Worksheet
Set critRem = Worksheets("Enterprise - score")
Set listRem = Worksheets("Sheet1")
Dim critRemID, listRemID, cell, matchedCell As Range
With critRem
Set critRemID = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
End With
With listRem
Set listRemID = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
End With
For Each cell In critRemID
Set matchedCell = listRemID.Find(cell.Value)
If matchedCell Is Nothing Then 'ID is not found
'Do nothing
Else 'ID is found, matchedCell is pointed to column A now
cell.Offset(0, 29).Resize(1, 10) = matchedCell.Offset(0, 89).Resize(1, 10)
'offset(0,29) means offsetting right 29 columns
'resize(0,10) means resizing the range with 1 row and 10 columns width
'feel free to change the number for your data
End If
Next cell
Note: If you are confused about offset().resize()
, there is another approach. cell.Row
gives you the row that the data should be written into, and matchedCell.Row
gives you the row that the ID matched. So you can access certain cell by something like listRem.Range("D" & matchedCell.Row)
If as you say both sheets have the same IDs, then why not use a Vlookup function to bring the data into Sheet1, then simply copy the results and paste as values so you get rid of the formula on them cells?
Something like a loop running:
For i = 1 to LastRow
Sheet1.cells(i, YourColumnNumber).value = "=VLOOKUP(RC[-1], Sheet2!R1:R1048576, 3, False)"
Next i
Tried to do it using the loop.
Sub Anser()
Dim critRemID As Range
Dim listRemID As Range
Dim critRemIDstart As Range
Dim listRemIDstart As Range
'::::Change Sheet names and column numbers:::::
Set critRemID = Worksheets("Sheet1").Cells(2, 1)
Set listRemID = Worksheets("Sheet2").Cells(2, 1)
Set critRemIDstart = Worksheets("Sheet1").Cells(2, 2)
Set listRemIDstart = Worksheets("Sheet2").Cells(2, 2)
Dim i, j As Integer
i = 0
j = 0
Do
If critRemID.Offset(i, 0) = listRemID.Offset(j, 0) Then
critRemIDstart.Offset(i) = listRemIDstart.Offset(j)
i = i + 1
j = 0
Else
j = j + 1
End If
Loop While critRemID.Offset(i, 0) <> ""
End Sub
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.