I'm copying Sheet 1 to multiple identical workbooks in 1 folder. When I do this, the formulas in Sheet 1 remain dependent on the source workbook. I'd like to remove that dependency so I'm trying to find and replace the connecting strings with "". Due to the volume of files, it isn't feasible to do so 1x1 - which is why I'm looking for some VBA help.
I do have the code to copy Sheet1 to all workbooks in the folder. And I have found a piece of code RE: find and replace. But I dont know how to piece them together.
Any help would be appreciated.
Option Explicit
Public Sub CopySheetToAllWorkbooksInFolder()
Dim sourceSheet As Worksheet
Dim folder As String, filename As String
Dim destinationWorkbook As Workbook
'Worksheet in active workbook to be copied as a new sheet to the destination woorkbook
Set sourceSheet = ActiveWorkbook.Worksheets("Sheet1")
'Folder containing the destination workbooks
folder = "'C:\Users\FOLDERLOCATION\[FILENAME.xlsm]"
filename = Dir(folder & "*.xls", vbNormal)
While Len(filename) <> 0
Debug.Print folder & filename
Set destinationWorkbook = Workbooks.Open(folder & filename)
sourceSheet.Copy before:=destinationWorkbook.Sheets(1)
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Wend
End Sub
the code I have for the copy/replace
fnd = "'C:\Users\FOLDERLOCATION\[FILENAME.xlsm]"
rplc = ""
I take it that in your source workbook in Sheet1
there are Formulas that reference cells on other sheets in the source workbook. Lets say =Sheet2!A1
or similar. On the assumption that the destination workbooks contain sheets with the same names as those referenced in the source workbook, you can use the Workbook.ChangeLink
method to update the copied sheets.
Note:
1. your folder
value looks suspect. I've substituted a valid path on my system to demo the correct format.
2. I've used *.xls*
in my Dir function. Revert to *.xls
if you want.
Public Sub CopySheetToAllWorkbooksInFolder()
Dim sourceWB As Workbook, sourceSheet As Worksheet
Dim folder As String, filename As String
Dim destinationWorkbook As Workbook
'Worksheet in active workbook to be copied as a new sheet to the destination woorkbook
Set sourceSheet = ActiveWorkbook.Worksheets("Sheet1")
Set sourceWB = sourceSheet.Parent
'Folder containing the destination workbooks
folder = "C:\Data\Temp\SO\Test\" ' "'C:\Users\FOLDERLOCATION\[FILENAME.xlsm]"
filename = Dir(folder & "*.xls*", vbNormal)
Do While Len(filename) <> 0
Debug.Print folder & filename
Set destinationWorkbook = Workbooks.Open(folder & filename)
sourceSheet.Copy Before:=destinationWorkbook.Sheets(1)
destinationWorkbook.ChangeLink _
Name:=sourceWB.Name, _
NewName:=destinationWorkbook.Name, _
Type:=xlExcelLinks
destinationWorkbook.Close True
filename = Dir() ' Get next matching file
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.