Hello,
I have the following code which does what I want which is it loops through a directory of excel files insetrs a worksheet based on a name I give it and copies and pasted data from the worksheet called Currentmonth into the new worksheet and deltes the data in CurrentMonth.
However it is not stopping at the last excel file in the folder. Insdtead it opens up the first file again and tries to repaeat the same code. Fortunatley it errors out for me (Cannot rename a sheet to the same name as another sheet...) I The reason is due to it not stopping after the last file in the directory, I do I stop the loop after it does what it is supposed to?
Thanks
Sub Dynamic()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Long
Dim Answer$
Answer = Application.InputBox("Enter New Sheet Name: MM-01-YYYY")
'Which folder?
fPath = "F:\Billing Summary\Retail\"
'Turn of the screen
Application.ScreenUpdating = False
'Loop until we run out of files
Do While fName <> ""
'Open the workbook
Set wb = Workbooks.Open(fPath & fName)
ActiveWorkbook.Sheets.Add.Name = Answer
Sheets(Answer).Activate
Application.CutCopyMode = False
Sheets("CurrentMonth").Select
Sheets("Currentmonth").Range("A1:O10000").Select
Selection.Copy
Sheets(Answer).Activate
ActiveSheet.Paste Destination:= _
Sheets(Answer).Range("A" & 1)
Sheets(Answer).Move before:=Sheets(4)
Sheets("CurrentMonth").Select
Sheets("Currentmonth").Range("A1:O10000").Select
Selection.ClearContents
Sheets("currentmonth").Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
'Increment count for feedback
i = i + 1
'Get next file name
fName = Dir()
Loop
'turn screen back on
Application.ScreenUpdating = True
'Give feedback
MsgBox "All done." & vbNewLine & "Number of files changed: " & i, vbOKOnly, "Run complete"
End Sub
I have the following code which does what I want which is it loops through a directory of excel files insetrs a worksheet based on a name I give it and copies and pasted data from the worksheet called Currentmonth into the new worksheet and deltes the data in CurrentMonth.
However it is not stopping at the last excel file in the folder. Insdtead it opens up the first file again and tries to repaeat the same code. Fortunatley it errors out for me (Cannot rename a sheet to the same name as another sheet...) I The reason is due to it not stopping after the last file in the directory, I do I stop the loop after it does what it is supposed to?
Thanks
Sub Dynamic()
Dim fPath As String
Dim fName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Long
Dim Answer$
Answer = Application.InputBox("Enter New Sheet Name: MM-01-YYYY")
'Which folder?
fPath = "F:\Billing Summary\Retail\"
'Turn of the screen
Application.ScreenUpdating = False
'Loop until we run out of files
Do While fName <> ""
'Open the workbook
Set wb = Workbooks.Open(fPath & fName)
ActiveWorkbook.Sheets.Add.Name = Answer
Sheets(Answer).Activate
Application.CutCopyMode = False
Sheets("CurrentMonth").Select
Sheets("Currentmonth").Range("A1:O10000").Select
Selection.Copy
Sheets(Answer).Activate
ActiveSheet.Paste Destination:= _
Sheets(Answer).Range("A" & 1)
Sheets(Answer).Move before:=Sheets(4)
Sheets("CurrentMonth").Select
Sheets("Currentmonth").Range("A1:O10000").Select
Selection.ClearContents
Sheets("currentmonth").Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
'Increment count for feedback
i = i + 1
'Get next file name
fName = Dir()
Loop
'turn screen back on
Application.ScreenUpdating = True
'Give feedback
MsgBox "All done." & vbNewLine & "Number of files changed: " & i, vbOKOnly, "Run complete"
End Sub