我想知道这是否可行,所以请不要将此视为“为我创建项目”帖子
我们每季度向客户发送一次使用报告(自购买许可证之日起每90天一次)。此报告包含包含30多列原始数据的主要表格。
我想创建一个简单的>将新报告拖到文件夹中>将主要工作簿的结果粘贴到报告中
创建这个显然很简单,但我想:
所以我问 VBA 是否可以获得第3,4和5点?我根本不熟悉它,最近才发现excel中存在宏的可能性。
答案 0 :(得分:0)
下面我有一个宏,可以逐个打开文件夹中的所有工作簿,你会注意到我在哪里注释了一个部分,在这里你可以输入你的代码并对给定的工作簿执行操作。这将循环遍历文件夹中的每个工作簿,直到没有任何剩余。
Sub ImportMacro()
'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 FldrPicker As FileDialog
'Values in sheets'
Dim VolatilityPortfolio As String
Dim ColValue As String
VolatilityPortfolio = "VolatilityPortfolio"
'Optimize Macro Speed'
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
'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 = "*.xl??"
'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)
'-------------------- Below is the worksheet macro --------------------------'
'To open the currentworkbook
Workbooks(myFile).activate
'---------------------------- Above is the worksheet macro ----------------------- '
'Save and Close Workbook as CSV'
wb.Close SaveChanges:=True
'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