我一直在尝试创建一个宏来打开存储在特定文件夹中的所有“.csv”文件。文件按日期排列。从Excel工作表中的单元格条目中读取要打开的特定文件。每个文件都必须单独修改,例如,将数据透视表制作出来。我已经能够编写一个宏,按照单元格值打开文件并创建一个数据透视表。但我的问题是,对于每个打开的文件,正在创建“新工作簿”。我想打开同一工作簿中的文件并相应地修改它们。有帮助吗?以下是我用于上述任务的代码。
Sub Open_Workbook1()
Dim MyFile As String
Dim Date_Read As String
Date_Read1 = Cells(1, 1).Value
myfile1 = "C:\Ashok\ExcelFiles\" & Date_Read1 & ".csv"
Workbooks.Open myfile1
Pivot1 // VBA code to make a pivot table out of the above file
Out_FileName1 = "C:\Ashok\ExcelFiles\" & Date_Read1 & ".xlsx"
ActiveWorkbook.SaveAs Filename:=Out_FileName2, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
Date_Read2 = Cells(1, 2).Value
myfile2 = "C:\Ashok\ExcelFiles\" & Date_Read2 & ".csv"
Workbooks.Open myfile2
Out_FileName2 = "C:\Ashok\ExcelFiles\" & Date_Read2 & ".xlsx"
Pivot2 // Same code as above one as the two files are similar
ActiveWorkbook.SaveAs Filename:=Out_FileName2, FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
End Sub