VBA Index Match & Paste???

Mr B J B

New Member
Joined
Dec 11, 2018
Messages
9
Hi All,

I've currently got 2 worksheets in a single workbook which interact with each other. Worksheet1 pulls data from worksheet2 by using an index match, but the problem I have with this is when the data is changed in worksheet 2 this then knocks on to worksheet1. I have the excel formulas working fine but I think as I require the data to become permanent in worksheet1 I will need to change the formulas to VBA code and add a copy and paste command??

Please can someone help me with this as I'm struggling to translate what I require into VBA. The formula that pulls the information through looks like this:

INDEX('Worksheet2'!E:E,MATCH('Worksheet1'!W2,'Worksheet2'!C:C,0))

The cells with data in to be matched in Worksheet1 are W2 to W10000 and in Worksheet2 are C3 to C100.

Many Thanks
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Put the following macro in a module

Code:
Sub Macro10()
    With Range("X2:X" & Range("W" & Rows.Count).End(xlUp).Row)
        .Formula = "=INDEX(Worksheet2!E:E,MATCH(W2,Worksheet2!C:C,0))"
        .Value = .Value
    End With
End Sub

Change the "X" in "X2:X" to the column where you want to put the result.

Execute the macro on the sheet1.

Test and tell me.
 
Last edited:
Upvote 0
Hi Dante,

I tried as you suggested but I'm getting a Run-time error '1004': Application-defined or object-defined error, which when I run debugger stops at the line starting With. The code I used is:

Sub Macro10()
With Range("AI2:AI" & Range("W" & Rows.Count).End(x1up).Row)
.Formula = "=INDEX(Worksheet2!E:E,MATCH(W2,Worksheet2!C:C,0))"
.Value = .Value
End With
End Sub
 
Upvote 0
In this part : End(x1up) you put the number 1, must be a letter l: End(Xlup)
;)
 
Upvote 0
Hi Dante,

The code seems to be overwriting the formula also when a value isn't returned.

So I changed the formula to =IFERROR(INDEX('Worksheet2'!E:E,MATCH('Worksheet1'!W2,'Worksheet2'!C:C,0)),"") to try and remove the #N/A but when I do this the code is now running an error on the foumla line:

.Formula = "=IFERROR(INDEX('Worksheet2'!E:E,MATCH('Worksheet1'!W2,'Worksheet2'!C:C,0)),"")"

Please can you advise on where I have gone wrong.

Thanks

Brendan
 
Upvote 0
You will need to "double-up" on the quotes inside the formula, like
"=IFERROR(INDEX('Worksheet2'!E:E,MATCH('Worksheet1'!W2,'Worksheet2'!C:C,0)),"""")"
 
Upvote 0
Thanks Fluff, the error has gone now. However when the data is changed in Worksheet 2 and the data that has been matched is no longer there what has been pulled into column AI previously is overwritten.

I have tried using ISBlank in the formula so only the the index match formula applies to blank cells in column AI but this is not working.

Is it possible to do this?
 
Upvote 0

Forum statistics

Threads
1,214,592
Messages
6,120,433
Members
448,961
Latest member
nzskater

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