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
 

Some videos you may like

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
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
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
55,916
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,554
Messages
5,596,814
Members
414,104
Latest member
imamalidadashzada

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
Top