Managing variable Workbook name VBA

ExcelFind

New Member
Joined
Apr 6, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am looking for a VBA code that reads the active workbook name, in order to use it later as reference in order to activate/open the same workbook in question.

Background: The VBA code is activated in "workbook1.xlsm" via a script button and exports data from SAP into a temp. Excel spreadsheet, this data is then copied to "Sheet1" in "workbook1.xlsm".

Issue: When changing the workbook name of "workbook1.xlsm", for example to "workbook2.xlsm", I have to update the workbook reference name in VBA code as well in order to prevent an error.

Questions:
  1. Can you help me out with a code that is reading the active workbook name, in order to use it later when the data is copied from the temp. Excel workbook?
  2. Can you help me out with a code that is (first of all) checking if "Sheet1" is present in the workbook?
From this point in the code below, the temporary Excel spreadsheet has been created and data is copied in cell A2 of Sheet1 in workbook1.xlsm
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("workbook1.xlsm").Activate
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I would recommend using workbook objects.
Here is a simple example showing you how to assign the active workbook to a workbook object, and then showing you how you can get its name:
VBA Code:
    Dim wb As Workbook
    Set wb = ActiveWorkbook
    MsgBox wb.Name
And once you set it equal to a workbook variable, you should be able to easily activate it like this:
VBA Code:
wb.Activate

To check to see if a particular sheet name exists, see this code here: How To Check If A Worksheet Exists Using VBA | How To Excel
 
Upvote 0
Solution
Hello Joe,

Thanks for the quick reply, I tried but the VBA runs into error. Can you help me out with the code?

I added this code in the first part of the VBA script:

VBA Code:
Dim wb As Workbook
Set wb = ActiveWorkbook

How should I now write the code for activating/calling the workbook using the workbook object, the code below is not working...

VBA Code:
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows(wb).Activate
Sheets("Sheet1").Select
Range("A2").Select
ActiveSheet.Paste
 
Upvote 0
Hello Joe,

Thanks for the quick reply, I tried but the VBA runs into error. Can you help me out with the code?

I added this code in the first part of the VBA script:



How should I now write the code for activating/calling the workbook using the workbook object, the code below is not working...
You need to look a little closer at what I posted, specificially the second code block.
You just use:
VBA Code:
wb.Activate
 
Upvote 0
Sorry, I was to fast with this and didn't noticed the second line! It is working. Many thanks!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,620
Messages
6,179,927
Members
452,949
Latest member
beartooth91

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