Excel - VBA to find filename and replace
Asked By BeSmart
27-Jan-10 03:16 AM

Hi
I just found the answer to "Find and Replace in VBA" and tried to adapt it
for what I am doing, but it is more difficult so I need some help please.
I have multiple worksheets in an excel 2007 workbook - a monthly report.
Within the worksheets I have many cells that link to last months workbooks -
(i.e I compare last month figures to this months figures).
I need the macro to do the following:
- Create an Input box for the user to "enter the name ofTHE OLD months
workbook"
- Create an Input box for the user to "enter the name of THE NEW months
workbook"
- Excel finds cells with links containing the OLD workbook name and replaces
it with the NEW workbook name
The problem:
After the user enters the NEW name excel asks the user to also select the
new workbook from a window and sometimes requires the worksheet to be
selected as well.
Is there any way to stop this requirement, but to ensure that the revised
links work?
I tried including "Application.ScreenUpdating = False" and
for 9 worksheets).
Also - is there a way to enter the Old and New workbook names once instead
of once for every worksheet in the workbook?
Old worksheet name:
.../[Plan for Restructuring and Rebudgeting as at 06_10_09 XP
version.xls]/Monthly Detailed P&L
New worksheet name:
.../[Plan for Restructuring and Rebudgeting as at 07_10_09 XP
version.xls]/Monthly Detailed P&L
No other details in the links change (i.e. locations like the folder names,
worksheet name and cell etc are in exactly the same place in the new workbook
so they do not need to change)
Current code:
Sub Replace()
Dim WS As Worksheet
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each WS In Worksheets
WS.Cells.Replace What:=InputBox("Enter prior workbook words to be replaced
in links"), Replacement:=InputBox("Enter new workbook words to be entered
into links"), _
LookAt:=xlPart, SearchOrder:=xlByColumns, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Next
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
--
Thank for your help in Advance
BeSmart
Link.TextToDisplay
(1)
Hyperlinks
(1)
Excel
(1)
Application.DisplayAlerts
(1)
Worksheet
(1)
Workbook
(1)
Macro
(1)
VBA
(1)
Jeff replied to BeSmart
Hi BeSmart, If I completely understand you want to loop though the workbook
adn each sheet in that workbook updating your hyperlinks. Right? If thats the
case try somthing like this.
Sub UpdateLinks()
Const FolderPath As String = "\\NetworkShare\YourFolder\YourSubfolder\"
Dim aWorkbook As Workbook
Dim aWorksheet As Variant
Dim OldFile As String
Dim TargetFile As String
Dim Link As Hyperlink
Set aWorkbook = ThisWorkbook
OldFile = InputBox("Enter OldFile Name")
TargetFile = InputBox("Enter File Name")
'Loop through worksheets
For Each aWorksheet In aWorkbook.Worksheets
'Loop through Hyperlinks in worksheet
For Each Link In Worksheets(aWorksheet.Name).Hyperlinks
'Check for oldFile Name
If InStr(1, Link.TextToDisplay, OldFile, vbTextCompare) > 0 Then
With Link
'Update Hyperlink info
.Address = FolderPath + TargetFile
.TextToDisplay = FolderPath + TargetFile
End With
End If
Next
Next
End Sub
BeSmart replied to Jeff

Hi Jeff
Thanks for your code - however I am not using "Hyperlinks"...
I have formulas that include links in their calculations e.g.
Cell A5 formula is:
='C:\Documents and Settings\Bob.JOB-Bob\My Documents\Bob
Personal\Inventors\Meetings\[XClient Plan for Restructuring and Rebudgeting
as at 06_10_09 XP version.xls]Detailed Report'!$U$18+$C83
The macro needs to prompt the user to enter the OLD workbook name (above) as
and then
enter the new workbook name e.g. "[XClient Plan for Restructuring and
Rebudgeting as at 07_10_09 XP version.xls]" (the date changed in the
filename).
Nothing else changes changes in the formula.
With the code I used before wanted the user to select the file and sometimes
the worksheet as well - for every worksheet...
We only want to change the workbook name once (when the user enters the two
names into the input boxes) - without having to also navigate through to the
actual file in a FILE/OPEN window for each worksheet.
Hopefully this will only be a small change to the code you have already
written.
--
Thank again for your help - I really appreciate it.
BeSmart

Code to change part of a formula across worksheets Excel Hi all - I hope someone can help with this. . . I have formulas that include links (not hyperlinks) in their calculations e.g. Cell A5 formula is: = 'C: \ Documents and Settings \ Bob.JOB Earlier I got some help from "Jeff " (the following code) - but it is looking for Hyperlinks, not formula links and therefore it is not working - but I understand the logic of what it is doing. . . Can I change it from looking for hyperlinks to looking for links to external workbooks in a formula? : Sub UpdateLinks() Const FolderPath As Enter New File Name") ''''Loop through worksheets For Each aWorksheet In aWorkbook.Worksheets '''''Loop through Hyperlinks in worksheet For Each Link In Worksheets(aWorksheet.Name).Hyperlinks ''''''Check for oldFile Name If InStr(1, Link.TextToDisplay, OldFile, vbTextCompare) > 0 Then With Link '''''Update Hyperlink info .Address = FolderPath + TargetFile .TextToDisplay = FolderPath + TargetFile
Excel mit Hyperlink auf Open Office Dokument Excel Hallo liebe Experten, ich habe ein kleines Problem in Zusammenarbeit von Excel mit Open Office Text-Dokumenten. Mit nachfolgendem Makro lese ich aus der Spalte K einen _" & var_jahr & "-" & var_monat & "-" & var_tag & link = pfad & datei_name Range("L1").Value = link Range("L1").Select ActiveSheet.Hyperlinks.Add Anchor: = Selection, Address: = link, TextToDisplay: = link With Selection Selection.Hyperlinks(1).Follow NewWindow: = False, AddHistory: = True End With Range("L1").Select With Selection Selection.Hyperlinks.Delete End With Range("L1").Select Range("L1").Clear Range(adr).Select End Sub Ich
mailTo hyperlinks in Excel? Excel Excel creates bogus "mailTo" hyperlinks on entering some simple cell formulas like @sum. I know how to remove individual hyperlinks. Is there a way to prevent this silly behavior. Thanks. Excel Miscellaneous Discussions Excel (1) Don't use @sum() Try using = sum() - - Dave Peterson keywords: mailTo, hyperlinks, in, Excel
excel Excel excel Excel Discussions Excel (1) ExcelSalesAid (1) GuillettMicrosoft (1) SalesAid (1) Dguillett1 (1) I will certainly try to - - Don Guillett Microsoft MVP Excel SalesAid Software dguillett1@austin.rr.com keywords: excel description: excel