Match dates from different worksheets

Wheelie686

New Member
Joined
Oct 27, 2012
Messages
11
The following code works fine to match a date on Sheet2 with the date in Sheet1!A1 and paste accordingly. However I tried to modify it so the paste destination is on another workbook but nothing seems to happen.

VBA Code:
Sub test()
    Dim x
    x = [match(Sheet1!A1,Sheet2!1:1,0)]
    If IsNumeric(x) Then Sheets("Sheet2").Cells(6, x).Resize(4).Value = Sheets("Sheet1").[A2:A5].Value
End Sub

Code:
Sub test2()
    Dim x
    Dim Calc As Workbook
    Dim Why As Workbook
    
    Set Calc = ThisWorkbook
    Set Why = Workbooks("Book2")
    
    x = [match(Calc.Sheet1!A1,Why.Sheet2!1:1,0)]
    If IsNumeric(x) Then Why.Sheets("Sheet2").Cells(6, x).Resize(4).Value = Calc.Sheets("Sheet1").[A2:A5].Value
End Sub


Does anybody know what I've done wrong in Test2
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Your code is asking Excel to evaluate a formula containing Calc.Sheet1 and Why.Sheet2.

Both mean nothing to Excel, hence the line will error, x will take an error value (it's a Variant data type by default), and IsNumeric(x) will be FALSE.

Here's one way you could get it to work: x = [match(Sheet1!A1,[Book2]Sheet2!1:1,)]
 
Upvote 0
yeah I set Calc and Why as variables. The function of Book1 is to be a calculator for some work stuff and Why because Why is the name of another document but to keep it simple, I just called them Book1 and Book2.

I appreciate the help since it now does exactly what I want. However I'm now stuck with the name Book2. Where in the code would I change the name? I know the name and would never change, it's just Book1 that has a variable name. I've changed it a few times and it always works but Book2 has to be Book2.

VBA Code:
Sub test3()
    Dim x
    Dim Calc As Workbook
    Dim Why As Workbook
    
    Set Calc = ThisWorkbook
    Set Why = Workbooks("Book2")
    
    x = [match(Sheet1!A1,[Book2]Sheet2!1:1,)]
    If IsNumeric(x) Then Why.Sheets("Sheet2").Cells(6, x).Resize(4).Value = Calc.Sheets("Sheet1").[A2:A5].Value
End Sub
 
Upvote 0
You would need to change Book2 to the new name wherever it appears and so in both these lines.

VBA Code:
    Set Why = Workbooks("Book2")
    
    x = [match(Sheet1!A1,[Book2]Sheet2!1:1,)]
 
Upvote 0
Solution

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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