Macro Referencing a Changing Filename

LauraSophie

New Member
Joined
Nov 14, 2017
Messages
7
Hi,

I have a macro that opens a specific file, opens it, takes some data and then closes it again. The premise is there are so many files that need opening that it will do them one by one and then close them afterwards. The rest of the macro works completely fine but I want to be able to have the filename changeable without editing the macro.


Part of the macro is:

Workbooks.Open Filename:= _
"H:\FOLDER\SUBFOLDER\Filename.xlsx" _

, UpdateLinks:=3
Windows("Book1").Activate
Columns("D:D").Select
Selection.Copy

Do you know of a way to have the filename referenced in a cell within the worksheet as opposed to being called out specifically in the macro? I have a CELL function included in the worksheet that gives the exact filename but is there any way of getting the macro to look for this instead of having to specify it? That way if the end user updates the filename (probably with edit links) in the cell on the worksheet they don't need to go in an amend the macro?

Thanks!

 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
If the entire file path is in Sheet1 cell A1 then...
Code:
Workbooks.Open Filename:= CSTR(Sheets("Sheet1").Range("A" & 1).Value
Also, the use of selection and activate are usually not required and slow code execution. For example, if your copying and pasting D1:D10 from sheet1 to sheet2....
Code:
Sheets("Sheet1").Range("D1:D10).Copy Destination:=Sheets("Sheet2").Range("D" & 1)
Application.CutCopyMode = False
HTH. Dave
 
Last edited:
Upvote 0
Thank you! This is great!

It does only work when the subject cell has the file name typed as opposed to being calculated by a formula. I don't suppose you know a way around this? At the moment I am formatting the file name using: =SUBSTITUTE(SUBSTITUTE(LEFT(E4,SEARCH("]",E4,1)),"[","",1),"]","",1). Where E4 contains an =CELL(filename) formula.

Also, I'm not sure what you mean: "Also, the use of selection and activate are usually not required and slow code execution". Apologies - I'm not very advanced at VBA. It's just something I've been trying to learn by doing a bit more recently.

Thanks!
 
Upvote 0
It shouldn't matter whether U type the file path or generate it with a formula. I'm guessing the formula doesn't produce the exact file name. Check to make sure there are no " " (blank spaces) and that the "/" (file separator) and file extensions (ie. ".xlsm") are correctly outputted by the formula. As for the selection and activation, I was trying to help U learn and provided U with an example of copying and pasting without the use of activation or selection. Perhaps part of your formula generated contains integers and not strings which are required for file paths. That's why the previous post re. CSTR which forces the data to be interpreted as a string. I see that I missed a bracket above, So if U correct the sheet name and cell column and row in the following code for your formula output if should work.
Code:
Workbooks.Open Filename:= CSTR(Sheets("Sheet1").Range("A" & 1).Value)
U can also do this to check the formula output...
Code:
Msgbox CSTR(Sheets("Sheet1").Range("A" & 1).Value) & " characters: " and Len(CSTR(Sheets("Sheet1").Range("A" & 1).Value))
This will tell U what the outputted file path is and how many characters so U can check for invisible " " blank characters.
Dave
 
Upvote 0
Thank you! I figured out what the problem was and it was a rookie mistake!

The formula had the file path being outputted correctly but becuase the macro opens the file, the formula is #N/A until the file has been opened! Sorted - thank you!
 
Upvote 0

Forum statistics

Threads
1,214,423
Messages
6,119,398
Members
448,892
Latest member
amjad24

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