【问题标题】:Excel merge range from Macro run XLSB files into oneExcel 将宏运行 XLSB 文件中的范围合并为一个
【发布时间】:2015-05-22 20:14:50
【问题描述】:

我有多个(有时超过 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 Which:=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

【问题讨论】:

  • 如果所有工作簿都具有相同的宏名称,Call WorkBk.&lt;macro name here&gt; 会为您工作吗?
  • 我最终得到一个运行时错误“438”:对象不支持此属性或方法。如果 xlsb 文件中的宏被称为 Auto_Open() 代码应该是 Call WorkBk.Auto_Open ?
  • 您可能需要限定它所在的代码模块。Call WorkBk.&lt;module&gt;.Auto_Open
  • Application.Run _ "'" & FileName & "'!auto​​_open"

标签: excel merge vba


【解决方案1】:

最后的答案是:

      Application.Run _
    "'" & FileName & "'!auto_open"

【讨论】:

    猜你喜欢
    • 2019-03-21
    • 2015-05-26
    • 2020-06-12
    • 2021-02-24
    • 2022-11-03
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    相关资源
    最近更新 更多