I am just now learning vba and am trying to replicate an index/match formula I use in Excel. Here is the excel code I use:
=((IF(ISERROR(INDEX(sheet2!$J$1:$J$65536,MATCH(1,INDEX((sheet2!$M$1:$M$65536=$A7010)*(sheet2!$N$1:$N$65536=$B7010),0,1),0))),0, (INDEX(sheet2!$J$1:$J$65536,MATCH(1,INDEX((sheet2!$M$1:U$65536=$A7010)*(sheet2!$N$1:$N$65536=$B7010),0,1),0))))))
I have created the following code by trial and error and by reading posts, so I am not really sure what each part means. It works really well but has one fatal flaw (for my purposes). It only copies the formula. I really need it to copy the value from sheet 1, column "c" to sheet 2, column "c".
Sub index_match()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long
'sh1 as source sheet, sh2 as destination sheet
Set sh1 = Worksheets("Sheet2")
Set sh2 = Worksheets("Sheet1")
'search by row
lastrow1 = sh1.Cells.SpecialCells(xlCellTypeLastCell).Row
lastrow2 = sh2.Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 1 To lastrow1
For j = 1 To lastrow2
'match source column in quotes against destination column in quotes. may add more criteria, but only as lines ending in "And _"
If sh1.Cells(i, "a").Value = sh2.Cells(j, "a").Value And _
sh1.Cells(i, "b").Value = sh2.Cells(j, "b").Value Then
'copy source column in quotes to destination column in quotes.
sh1.Cells(i, "c").Copy sh2.Cells(j, "c")
sh1.Cells(i, "d").Copy sh2.Cells(j, "d")
End If
Next j
Next i
End Sub
=((IF(ISERROR(INDEX(sheet2!$J$1:$J$65536,MATCH(1,INDEX((sheet2!$M$1:$M$65536=$A7010)*(sheet2!$N$1:$N$65536=$B7010),0,1),0))),0, (INDEX(sheet2!$J$1:$J$65536,MATCH(1,INDEX((sheet2!$M$1:U$65536=$A7010)*(sheet2!$N$1:$N$65536=$B7010),0,1),0))))))
I have created the following code by trial and error and by reading posts, so I am not really sure what each part means. It works really well but has one fatal flaw (for my purposes). It only copies the formula. I really need it to copy the value from sheet 1, column "c" to sheet 2, column "c".
Sub index_match()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long
'sh1 as source sheet, sh2 as destination sheet
Set sh1 = Worksheets("Sheet2")
Set sh2 = Worksheets("Sheet1")
'search by row
lastrow1 = sh1.Cells.SpecialCells(xlCellTypeLastCell).Row
lastrow2 = sh2.Cells.SpecialCells(xlCellTypeLastCell).Row
For i = 1 To lastrow1
For j = 1 To lastrow2
'match source column in quotes against destination column in quotes. may add more criteria, but only as lines ending in "And _"
If sh1.Cells(i, "a").Value = sh2.Cells(j, "a").Value And _
sh1.Cells(i, "b").Value = sh2.Cells(j, "b").Value Then
'copy source column in quotes to destination column in quotes.
sh1.Cells(i, "c").Copy sh2.Cells(j, "c")
sh1.Cells(i, "d").Copy sh2.Cells(j, "d")
End If
Next j
Next i
End Sub