Add worksheet to workbook using VBA
I am looking to copy an existing (already created worksheet) into about 500 workbooks (*.xlsx) that all reside in the same folder. I was able to cobble together the below code from various other topics on here but I am not able to get it to work.
Private Sub Command0_Click()
Dim file As String
Dim myPath As String
Dim wb As Workbook
Dim rng As Range
Dim wbMaster As Workbook
'if master workbook already opened
'Set wbMaster = Workbooks("ProjectBabelfish.xlsx")
'if master workbook is not opened
Set wbMaster = Workbooks.Open(CurrentProject.Path & "\ProjectBabelfish.xlsx")
Set rng = wbMaster.Sheets("Babelfish").Range("A1:CC200")
myPath = CurrentProject.Path & "\PLOGs\" ' note there is a back slash in the end"
file = Dir(myPath & "*.xlsx*")
While (file <> "")
Set wb = Workbooks.Open(myPath & file)
rng.Copy
With wb.Worksheets("Babelfish").Range("A1")
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
End With
wb.Close SaveChanges:=True
Set wb = Nothing
file = Dir
Wend
Application.CutCopyMode = False
End Sub
Other than simply copying the worksheet from workbook to another, the formulas need to reference cells in the new workbook. Also, I am trying to account for some of the workbooks being locked.
Something like this should work for you: