Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default search and replace within macros across workbooks?

We're migrating a large amount of files from one storage
system to another due to capacity constraints. We have a
set of excel files that contain VB macros with hard coded
links to the full URL path of some other files that are
also being moved. Does anyone know if there is a way to
do a global search and replace of text within Excel VB
macros for multiple Excel Spreadsheets?

I.E., original macro may contain text:
\\server1\directory\subdir\file.xls

We want to change any instances of that reference to:
\\server2\directory\subdir\file.xls

Thanks!
Mike

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 176
Default search and replace within macros across workbooks?

Mike,

Try the macro below on a subset of your files first, copied into the
folder C:\Test Folder. It will require that you set a reference to MS
VBA Extensibility.

Then after testing and to run it, change

.LookIn = "C:\Test Folder"
.SearchSubFolders = False

to

.LookIn = "C:\" 'or other drive
.SearchSubFolders = True 'to do all folders

HTH,
Bernie
MS Excel MVP

Sub FixAllFilesOnDrive()
Application.DisplayAlerts = False
With Application.FileSearch
.NewSearch
.LookIn = "C:\Test Folder"
.SearchSubFolders = False
.FileType = msoFileTypeExcelWorkbooks
If .Execute 0 Then
For i = 1 To .FoundFiles.Count
Workbooks.Open .FoundFiles(i)
ReplaceCodeInModule ActiveWorkbook
ActiveWorkbook.Save
ActiveWorkbook.Close
Next i
End If
End With
Application.DisplayAlerts = True

End Sub
Sub ReplaceCodeInModule(inBook As Workbook)
Dim myCode As String
Dim myComp As VBComponent
For Each myComp In inBook.VBProject.VBComponents
With myComp.CodeModule

myCode = .Lines(1, .CountOfLines)
myCode = Replace(myCode, _
"\\server1\directory\subdir\file.xls", _
"\\server2\directory\subdir\file.xls")

..DeleteLines 1, .CountOfLines
..InsertLines .CountOfLines + 1, myCode

End With
Next myComp
End Sub


"Mike Enfield" wrote in message
...
We're migrating a large amount of files from one storage
system to another due to capacity constraints. We have a
set of excel files that contain VB macros with hard coded
links to the full URL path of some other files that are
also being moved. Does anyone know if there is a way to
do a global search and replace of text within Excel VB
macros for multiple Excel Spreadsheets?

I.E., original macro may contain text:
\\server1\directory\subdir\file.xls

We want to change any instances of that reference to:
\\server2\directory\subdir\file.xls

Thanks!
Mike



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace Rockbear Excel Worksheet Functions 0 October 13th 08 08:25 AM
Search and Replace Rebecca New Users to Excel 2 June 5th 06 06:42 AM
Macros & search /replace Anthony Excel Worksheet Functions 2 February 12th 06 10:54 PM
Search and replace Subu Excel Worksheet Functions 4 June 9th 05 07:01 PM
Search and Replace text strings within Macros Chris Austin Excel Programming 1 September 5th 03 04:39 PM


All times are GMT +1. The time now is 12:08 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"