How to Copy/Paste as Value for SpecialCells

econjenny

New Member
Joined
Dec 27, 2010
Messages
2
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Try like this

Code:
sh1.Cells(i, "c").Copy
sh2.Cells(j, "c").PasteSpecial Paste:=xlPasteValues
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top