打开一个 sheet,从加载项执行一个函数,然后保存
Open a sheet, execute a function from an add-in, and save
我需要 运行 上千个 excel 文件 (.xlsx) 来获取数据。每个文件有很多sheet,在每个sheet中,单元格A1包含一个Excel加载项(晨星Excel加载项)的功能。现在,我必须手动打开每个文件。当加载项加载时,将执行单元格 A1 中的函数,并且单元格 A1 显示 "Processing..."。我将不得不等待几秒钟或几分钟才能将数据 return。 sheet 填满数据后,我会将其保存为 csv 文件。
如何自动执行此过程?
我写了一个宏来打开 excel 文件并将 sheet 保存为 CSV 文件。但是,它绕过了数据请求和下载过程。我添加了等待几秒钟的选项,但 Excel 文件以冻结状态打开,即加载项未加载且单元格 A1 中的功能未 运行。我怎样才能:
- 打开文件
- 确保加载项已加载
- 确保每个 sheet 运行s
的单元格 A1 中的函数
- 检查是否有数据。一种方法是检查单元格 A10 是否为空
- 将 sheet 另存为 CSV 文件
到目前为止,这是我的代码:
Sub morningstar_VBA()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim filename As String
Dim path_to_save As String
Dim FldrPicker As FileDialog
Dim w As Long
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
For w = 1 To Worksheets.Count
With Worksheets(w).Copy
'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
With ActiveWorkbook
filename = .Worksheets(1).Name
path_to_save = "E:\Morningstar_download\test\" & filename
.SaveAs filename:=path_to_save, FileFormat:=xlCSV
DoEvents
.Close savechanges:=False
End With
End With
Next w
wb.Close savechanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
包括下面的代码以刷新加载项,即确保加载项中的所有功能 运行。不确定其他加载项
Set cmd = Application.CommandBars("Cell").Controls("Refresh All")
cmd.Execute
我需要 运行 上千个 excel 文件 (.xlsx) 来获取数据。每个文件有很多sheet,在每个sheet中,单元格A1包含一个Excel加载项(晨星Excel加载项)的功能。现在,我必须手动打开每个文件。当加载项加载时,将执行单元格 A1 中的函数,并且单元格 A1 显示 "Processing..."。我将不得不等待几秒钟或几分钟才能将数据 return。 sheet 填满数据后,我会将其保存为 csv 文件。
如何自动执行此过程?
我写了一个宏来打开 excel 文件并将 sheet 保存为 CSV 文件。但是,它绕过了数据请求和下载过程。我添加了等待几秒钟的选项,但 Excel 文件以冻结状态打开,即加载项未加载且单元格 A1 中的功能未 运行。我怎样才能:
- 打开文件
- 确保加载项已加载
- 确保每个 sheet 运行s 的单元格 A1 中的函数
- 检查是否有数据。一种方法是检查单元格 A10 是否为空
- 将 sheet 另存为 CSV 文件
到目前为止,这是我的代码:
Sub morningstar_VBA()
'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim filename As String
Dim path_to_save As String
Dim FldrPicker As FileDialog
Dim w As Long
'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xlsx*"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(filename:=myPath & myFile)
'Ensure Workbook has opened before moving on to next line of code
For w = 1 To Worksheets.Count
With Worksheets(w).Copy
'the ActiveWorkbook is now the new workbook populated with a copy of the current worksheet
With ActiveWorkbook
filename = .Worksheets(1).Name
path_to_save = "E:\Morningstar_download\test\" & filename
.SaveAs filename:=path_to_save, FileFormat:=xlCSV
DoEvents
.Close savechanges:=False
End With
End With
Next w
wb.Close savechanges:=True
'Ensure Workbook has closed before moving on to next line of code
DoEvents
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
包括下面的代码以刷新加载项,即确保加载项中的所有功能 运行。不确定其他加载项
Set cmd = Application.CommandBars("Cell").Controls("Refresh All")
cmd.Execute