I want to take a name from a sheet (let’s say A2 in a sheet called ‘Names’) and search for that same name in another worksheet (say A2 in ‘Jobs’). After that name is found in the other worksheet I want to copy the value from the cell right next to it (still in ‘Jobs’ but B2) and return it to a different cell (E2) in the first sheet (‘Names’). I ultimately want to loop though all of the values in A1 of ‘Names’ and fill in the whole sheet.
I have gotten this far:
Sub fixThis()
Dim i As Long, j As Long, col1 As Long, col2 As Long, lastrow1 As Long, lastrow2 As Long
Dim sheetOne As String
Dim sheetTwo As String
col1 = 5
col2 = 1
sheetOne = "Names"
sheetTwo = "Job"
lastrow1 = Cells(Rows.Count, col1).End(xlUp).Row
lastrow2 = Cells(Rows.Count, col2).End(xlUp).Row
For i = 2 To lastrow1
For j = 2 To lastrow2
If sheetOne.Cells(i, col1).Value = sheetTwo.Cells(j, col2).Value Then
sheetOne.Cells(i, 6).Value = sheetTwo.Cells(j, 2).Value
End If
Next
Next
End Sub
It’s okay if you store the sheet names as strings. but when you use them you need to use them to reference the sheet object like this:
Sheets("Sheetname").Cells().ValueOr you can use a variable like this:
Finally if you really want to you can declare your own sheet object like
To keep all of your above code the same you need to try replacing
With
Then finally, if you’re working with multiple sheets, you need to ad dmore detail to these lines:
And change them to something like:
The final version would look something like this: