VBA to match cell contents across multiple sheets and copy data from a corresponding cell

lyntanos

New Member
Joined
Dec 6, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I'm trying to sort out a VBA script that will achieve this:
I have a value in Sheet2 A1 that the user can change.
I want to create a VBA script that will search all of the cells in column K of Sheet1 (from K3 to end of filled cells in column K) for a match of the value in Sheet2 A1 and if there is a match,
copy the value of the corresponding cell in column C from Sheet1 (Starting from C3) into column B in Sheet2.

Eg. if Sheet2 A1 has matches in Sheet1 K3 and K10, copy the contents of Sheet1 C3 and C10 to Sheet2 B2 and B3.

Thanks in advance
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
VBA code for macro.
VBA Code:
Sub GetValues()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim Lr&
Dim M
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
Lr = sh2.Range("K" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If('" & sh2.Name & "'!K3:K" & Lr & "='" & sh1.Name & "'!A1,'" & sh2.Name & "'!C3:C" & Lr & ",false))"), False, False)
sh1.Range("B:B").Clear
sh1.Range("B3").Resize(UBound(M) + 1, 1) = WorksheetFunction.Transpose(M)
End Sub
 
Last edited:
Upvote 0
VBA code for macro.
VBA Code:
Sub GetValues()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim Lr&
Dim M
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
Lr = sh2.Range("K" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If('" & sh2.Name & "'!K3:K" & Lr & "='" & sh1.Name & "'!A1,'" & sh2.Name & "'!C3:C" & Lr & ",false))"), False, False)
sh1.Range("B:B").Clear
sh1.Range("B3").Resize(UBound(M) + 1, 1) = WorksheetFunction.Transpose(M)
End Sub
Thanks for your time.
I had to reverse sheet1 & sheet2 in your code, but it worked perfectly.
 
Upvote 0
Try this code. I have changed sheet numbers.
VBA Code:
Sub GetValues()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim Lr&
Dim M
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
Lr = sh1.Range("K" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(('" & sh1.Name & "'!K3:K" & Lr & "='" & sh2.Name & "'!A1)*('" & sh1.Name & _
            "'!Q3:Q" & Lr & "='" & sh2.Name & "'!A2),'" & sh1.Name & "'!C3:C" & Lr & ",false))"), False, False)
sh2.Range("B:B").Clear
If UBound(M) >= 0 Then sh2.Range("B3").Resize(UBound(M) + 1, 1) = WorksheetFunction.Transpose(M)
End Sub
 
Upvote 0
Solution
Try this code. I have changed sheet numbers.
VBA Code:
Sub GetValues()
Dim sh1 As Worksheet, sh2 As Worksheet
Dim Lr&
Dim M
Set sh1 = Sheets("Sheet1"): Set sh2 = Sheets("Sheet2")
Lr = sh1.Range("K" & Rows.Count).End(xlUp).Row
M = Filter(Evaluate("Transpose(If(('" & sh1.Name & "'!K3:K" & Lr & "='" & sh2.Name & "'!A1)*('" & sh1.Name & _
            "'!Q3:Q" & Lr & "='" & sh2.Name & "'!A2),'" & sh1.Name & "'!C3:C" & Lr & ",false))"), False, False)
sh2.Range("B:B").Clear
If UBound(M) >= 0 Then sh2.Range("B3").Resize(UBound(M) + 1, 1) = WorksheetFunction.Transpose(M)
End Sub
Perfect, thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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