Macro does not work if file is renamed

1nk3d

Board Regular
Joined
May 31, 2016
Messages
51
Good morning everyone, I am a beginner when it comes to VBA and such. I have a problem that I feel is an easy fix for someone with more experience. My macro will not work if the file is renamed. Very quickly, the user clicks a button, and it downloads a preloaded file and loads it to the workbook. Can someone point me in the right direction?

Thank you
Code:
Sub Data()
'
' Macro4 Macro
'
'
    Workbooks.Open Filename:= _
        "excel file location"
    Range("A1:H1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("Tool.xlsm").Activate
    Sheets("datasheet").Select
    Range("B2").Select
    ActiveSheet.Paste
    Sheets("BCard").Select
    Application.WindowState = xlNormal
    Windows("data.xlsx").Activate
    Application.DisplayAlerts = False
    ActiveWindow.Close
    Sheets("BCard").Select
    Application.WindowState = xlMaximized
    
End Sub
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
It looks like they hard-coded the Excel file name "Tool.xlsm" into the Macro.
Instead of doing that, just have Excel dynamically capture the name of the file at the very top of your code, like this:

Code:
Dim srcWB as Workbook
Set srcWB = ActiveWorkbook
Then you can replace any hard-coded references activating the workbook later in your code to this:
Code:
srcWB.Activate
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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