T
TEB2
I have 484 files in the same directory. I want to loop through all the
workbooks and add a sheet called "Memo".
I have the code to add the sheet. How do I handle the error when it already
has a sheet named "Memo" and go to the next file?
Here's the code I'm using:
Sub Memo_Test()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
SaveDriveDir = CurDir
MyPath = "C:\Documents and Settings\Consultant\My
Documents\Partnerships\Carol Bynum\Test Files"
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
On Error GoTo CleanUp
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames <> ""
Set mybook = Workbooks.Open(FNames, UpdateLinks:=0)
basebook.Worksheets("Sheet1").Copy
after:=mybook.Sheets(mybook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = basebook.Name
On Error GoTo 0
mybook.Close True
FNames = Dir()
Loop
CleanUp:
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub
workbooks and add a sheet called "Memo".
I have the code to add the sheet. How do I handle the error when it already
has a sheet named "Memo" and go to the next file?
Here's the code I'm using:
Sub Memo_Test()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
SaveDriveDir = CurDir
MyPath = "C:\Documents and Settings\Consultant\My
Documents\Partnerships\Carol Bynum\Test Files"
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xls")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If
On Error GoTo CleanUp
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames <> ""
Set mybook = Workbooks.Open(FNames, UpdateLinks:=0)
basebook.Worksheets("Sheet1").Copy
after:=mybook.Sheets(mybook.Sheets.Count)
On Error Resume Next
ActiveSheet.Name = basebook.Name
On Error GoTo 0
mybook.Close True
FNames = Dir()
Loop
CleanUp:
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub