How to make changes in the target file if the vba codes are in a different file

Zain_inout

New Member
Joined
Sep 8, 2021
Messages
18
Office Version
  1. 2019
Platform
  1. Windows
My code is written in my macro workbook. I have to work on a different file every time with the same steps. So ideally I need a macro in a macro file and my target files will be different. Below is a code I have in my macro file.

Sub EEBalanceSummary()
On Error Resume Next
Dim FilePath As String
FilePath = Application.GetOpenFilename
Range("B6").Select

End Sub

I run this code from my macro file. It asks me to select the folder and then the file I need to work on. Now, instead of selecting "B6" cell in my newly opened target file, it selects "B6" in the same macro file. How can I get the code to work on my target file?
 

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)
something along these lines:
VBA Code:
Sub EEBalanceSummary()
On Error Resume Next
Dim FilePath As String
dim wb as workbook
FilePath = Application.GetOpenFilename
if not FilePath = "False" then set wb = Application.workbooks.open(Filepath)
if wb is nothing then exit sub
with wb
 .Activate
.Activesheet.Range("B6").Select
end with
End Sub
assign the newly opened workbook to a variable and then use it to refer to objects in it.
 
Upvote 0
Solution
something along these lines:
VBA Code:
Sub EEBalanceSummary()
On Error Resume Next
Dim FilePath As String
dim wb as workbook
FilePath = Application.GetOpenFilename
if not FilePath = "False" then set wb = Application.workbooks.open(Filepath)
if wb is nothing then exit sub
with wb
 .Activate
.Activesheet.Range("B6").Select
end with
End Sub
assign the newly opened workbook to a variable and then use it to refer to objects in it.
It worked! Thank you so much :)
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,150
Members
453,021
Latest member
Justyna P

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