Reference a workbook based on a cell value

AccountantHarry

New Member
Joined
Oct 13, 2019
Messages
18
Hi Experts

I have learnt and gained so much from this forum on VBA, hope someone will be so kind to help me again.

I have written a macro to copy data from one workbook to another (with multiple worksheets), the names for the target and source workbooks are different each month, eg Nov is V11 Sales, Dec is V12 Sales... I have a worksheet called Macro data where I have all the file paths with formulas, so users can change the month, the file paths are then changed automatically. The codes I have written can open the workbooks based on the cells in the Macro data worksheet, however, once the workbooks are opened, I can't seem to be able to get the codes to go back to the workbooks again, the error message is "Run-time error 91, Object variable or With block variable not set".
These are my codes excluding the opening workbooks codes and other stuff:
VBA Code:
   Sub Sales()
Dim sourceWb as Workbook
Dim targetWb as Workbook

sourceWb = Thisworkbook.Worksheets("Macro data").Range("B3").Value
targetWb = Thisworkbook.Worksheets("Macro data").Range("B4").Value

Windows("targetWb").Activate
Worksheets("For Macro").Cells(26.11).PasteSpecialxlPasteValues
Regards
 

Attachments

  • 1577726463233.png
    1577726463233.png
    21 KB · Views: 3
  • 1577727002645.png
    1577727002645.png
    19.8 KB · Views: 3
Last edited by a moderator:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
you defined sourceWb (and targetWB) as WORKBOOKS, yet you assigned a VALUE to them.
you should assign workbooks to workbooks:
set sourceWb = Thisworkbook
or
set sourceWb = workbooks("wbName")
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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