Find/Replace? to change a sheet name in a formula

MPBJR

Board Regular
Joined
Mar 28, 2007
Messages
143
I have a range on a sheet (Q89:S100) that I have formulas in to pull data from a file on a server. Every day this file gets updated and a new sheet is added with that days data on it, the sheet has a name format of 031819. I need to have a macro that runs at 5pm everyday that changes the sheet name in the formula to the newest sheet in the source file to get the most updated data.

For example, the formula I have in Q89 is:
='\\server\folder1\folder2\[source file.xlsx]031319'!B2

So the next day, i need to update all the formulas to pull data from 031419 (B2 will remain the same).

I've tried various macros for find & replace but can't get them to work.

I've set up 2 cells as a find value and replace value but can't get it to work.

I know how to set up the macro to run at a certain time, just can't figure out this find and replace part.

Any help would be greatly appreciated. Thanks

Code:
Sub UPDATE()
'
' UPDATE Macro
'

    
    
    
    Dim fnd As String
    Dim rplc As String

    fnd = Cells(100, "J").Value
    rplc = Cells(100, "K").Value
    Range("Q89:S100").Select
    Selection.Replace what:=fnd, Replacement:=rplc, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What exactly is in cells J100 & K100?
 
Upvote 0
Untested, but if you are changing the date every day - 7 days a week, maybe something like this:
Code:
Sub ChangeDate()
Dim lastDate As String, nextDate As String
lastDate = CStr(Format(Month(Date - 1), "00") & Format(Day(Date - 1), "00") & Format(Right(Year(Date - 1), 2), "00"))
nextDate = CStr(Format(Month(Date), "00") & Format(Day(Date), "00") & Format(Right(Year(Date), 2), "00"))
Range("Q89").Formula = Replace(Range("Q89").Formula, lastDate, nextDate)
End Sub
 
Upvote 0
In that case try
Code:
Sub UPDATE()
'
' UPDATE Macro
    Dim fnd As String
    Dim rplc As String

    fnd = Format(Cells(100, "J").Value, "mmddyy")
    rplc = Format(Cells(100, "K").Value, "mmddyy")
    Range("Q89:S100").Replace what:=fnd, Replacement:=rplc, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Just wanted to see if anyone could help with an add on to the above code. I now want to check that the sheet name exists in the external file that I'm pulling the data from before I run the find/replace code.
The name of the sheet would be the same value as the "rplc = Format(Cells(100, "K").Value, "mmddyy")" line in the above code. If the sheet name in k100 exists in the external doc, then I want to run the find replace code provided by Fluff, if it does not exist, I simply want the code to stop running. Thanks
 
Upvote 0
Will the other file be open when you run the code?
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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