我有多个(有时是100多个)xlsb文件,用户希望将Sheet8中的第14行从所有文件复制到一个工作簿/工作表中。
我能够执行此功能;但结果最终显示xlsb文件中所有计算字段的0'
xlsb文件是宏运行
在我打开文件的代码中看起来像这样: '这个可以打开,但不会通过宏运行 设置WorkBk = Workbooks.Open(FolderPath& FileName)
我用这个更新了代码;但是之后的下一行不会运行,我相信因为它正在寻找“SET”而我不确定如何执行此操作
'This one opens and runs macro but then fails at Set SourceRange
Workbooks.Open(FolderPath & FileName).RunAutoMacros Which:=xlAutoOpen
当我尝试添加.RunAutoMacros:= xlAutoOpen在第一个代码之后我得到一个编译错误:预期:语句结束
'This one works to open but doesn't run through Macro
Set WorkBk = Workbooks.Open(FolderPath & FileName).RunAutoMacros Which:=xlAutoOpen
以下是完整代码:
Sub MergeAllWorkbooks()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim auto_open As String
Application.Calculation = xlCalculationAutomatic
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\dredden2\Documents\SHAREPOINT ARCHIVING\PAGESETUP\TEST\"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 2
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = DIR(FolderPath & "*.xlsb")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
'This one works to open but doesn't run through Macro
Set WorkBk = Workbooks.Open(FolderPath & FileName)
' Set the cell in column A to be the file name.
SummarySheet.Range("A" & NRow).Value = FileName
' Set the source range to be A9 through C9.
' Modify this range for your workbooks.
' It can span multiple rows.
Set SourceRange = WorkBk.Sheets("Retrospective Results").Range("B14:BF14")
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
Set DestRange = DestRange.Resize(SourceRange.Rows.Count, _
SourceRange.Columns.Count)
' Copy over the values from the source to the destination.
DestRange.Value = SourceRange.Value
' Increase NRow so that we know where to copy data next.
NRow = NRow + DestRange.Rows.Count
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = DIR()
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
End Sub
答案 0 :(得分:0)
最终答案是:
Application.Run _
"'" & FileName & "'!auto_open"