【问题标题】:Pull data from multiple closed workbooks and sheets in excel从 Excel 中的多个关闭的工作簿和工作表中提取数据
【发布时间】:2022-08-22 15:54:42
【问题描述】:

我想从已关闭的多个工作簿和工作表中提取数据。 我可以使用以下公式提取数据:

=INDEX(\'C:\\Users\\Shubham\\Downloads\\Ex_Files_Excel_Macros_and_VBA_for_Beginners\\Ex_Files_Excel_Macros_and_VBA_for_Beginners\\Exercise Files\\[03_01 Undo.xlsx]Attendance\'!C:C, 6)

但我想使用 CONCATENATE 函数在单元格中提供路径让我们在 E5 中说,以便 E5 中的值如下所示:

\'C:\\Users\\Shubham\\Downloads\\Ex_Files_Excel_Macros_and_VBA_for_Beginners\\Ex_Files_Excel_Macros_and_VBA_for_Beginners\\Exercise Files\\[03_01 Undo.xlsx]Attendance\'!C:C

所以我想使用如下公式:

= INDEX(E5, 6)

其中 E5 定义了路径和范围,但这不会将 E5 中的值作为表数组。

我不能使用间接公式,因为它不适用于封闭的工作簿。我试图理解 VBA 代码,但没有成功。

如果有人可以帮助我解决此问题,我将不胜感激,因为我必须从多个工作簿和多个工作表中提取数据,并且必须使用对路径和范围的动态引用。

    标签: arrays excel indexing range


    【解决方案1】:

    我遇到了同样的问题,想从具有易失工作表地址的多个工作簿中提取数据。间接和连接有效,但仅在源文件打开时有效。没有可用于关闭文件的功能。 一个宏是我在这里找到的解决方案; https://www.mrexcel.com/board/threads/vba-to-pull-data-from-dynamically-named-closed-workbooks.984156/

    我的解决方案使用带有可变日期组件(日期是源文件名中的唯一变量)和可变工作表组件(我需要为一个人获取数据,其中每个人的名字都是名字)的源文件名组件列表每个文件中的工作表)。
    该列表如下所示;List of File Names

    I 和 K(名称)是易变的。通过让这些单元格引用单独的输入单元格以简化查询,这些在我的列表中进行了修改。输入您要查询的姓名和最新的工资单日期。它回顾一组周期。

    获取我想要的数据的公式是=COUNTIF(INDIRECT(CONCATENATE(H4,I4,J4,K4,L4)),"S") S 在每个文件的选项卡 Moe 中出现在D63:U63 范围内多少次。

    宏解决方案使用 I 列中的日期信息来打开所有源文件,如下所示(我添加了 cmets 以希望更清楚每个步骤的作用);

    Public Sub Macro_Name()
    
    '  Opens Timesheets files to allow "Indirect" formula to obtain data
    
    Dim r As Long
    '  I don't know what this does, but it is necessary
    
    Application.ScreenUpdating = False
    '  Stops screen updating to speed up operation
    
    Calculate
    '  Calculates to clear any previous data in the file.  File is set to manual calculation.
    
    With ThisWorkbook.ActiveSheet
        
        For r = 4 To .Cells(Rows.Count, "I").End(xlUp).Row
        '  Starting at row 4, uses data in column I in the command below until the end of the data.  You can't have any other data in column I.       
    
            Workbooks.Open Filename:="S:\Payroll\Weekly Timesheets " & .Cells(r, "I") & ".xlsm"
            '  Opens each file called   Weekly Timesheets [date].xlsm
        Next
        
    Windows("[The name of your working file].xlsm").Activate
    Calculate
    Range("B2").Select
    '  This calculates your working file and goes to a convenient "home" cell
    
    End With
    
    Dim wb As Workbook
    
    For Each wb In Application.Workbooks
        If Not wb Is ThisWorkbook Then
            wb.Close SaveChanges:=False
        End If
    Next
    '  This sub routine closes all open Excel files EXCEPT your working file.  Best not to have other files open as it is set NOT to save.
    
    Application.ScreenUpdating = True
    
    End Sub
    
    

    我找到了这个页面来设置手动计算的文件,这是我的首选。 https://excel.tips.net/T001988_Forcing_Manual_Calculation_For_a_Workbook.html

    我不确定这个解决方案是否有效,它可能不像我想要的那样优雅,但它对我有用。我希望它有所帮助。

    向提供上述核心代码的 MrExcel.com 上的 John_w 大喊大叫。我只是摆弄了一些东西,所以它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-22
      • 2011-06-04
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多