我正在尝试转到文件夹中的一组工作簿,并为每个名为当前日期的选项卡添加一个Tab。
下面的代码打开第一个文件并正确添加Tab,但不循环打开剩余的文件。
Sub LoopThroughFolder()
Dim MyFile As String, Str As String, MyDir As String, Wb As Workbook
Set Wb = ThisWorkbook
MyDir = "C:\Users\u558683\Desktop\QA VBA Project\LoopTabs\"
MyFile = Dir(MyDir & "*.xlsx")
ChDir MyDir
Do While MyFile <> ""
Workbooks.Open (MyFile)
TabName = Format(Date, "mmm-yyyy") 'Change the format as per your requirement
On Error GoTo AddNew
Sheets(TabName).Activate
Exit Sub
AddNew:
Sheets.Add , Worksheets(Worksheets.Count)
ActiveSheet.Name = TabName
ActiveSheet.Previous.Range("A1:AJ4").Copy Destination:=Range("A1")
ActiveSheet.Previous.Range("AL1:AN500").Copy Destination:=Range("AK1")
Loop
End Sub
答案 0 :(得分:0)
2个问题:
FOR EACH
。MyFile = Dir()
,只是在没有继续治疗的情况下退出该计划。固定代码:
Exit Sub