ExecuteExcel4Macro从已关闭的工作簿中获取范围/图表

时间:2013-06-07 09:48:06

标签: excel vba excel-vba adodb

我使用这些行从已关闭的工作簿中获取值:

Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & "R4C4"  
 Arg = CStr(Arg)
GetValue = ExecuteExcel4Macro(Arg)

除了循环之外还有其他方法可以从范围中获取值吗?循环解决方案正在运行但如果我可以直接使用ExecuteExcel4Macro获取范围会更清楚。我试图在Arg中输入一个范围,但它不起作用。它返回一个错误。

我对图表有同样的问题,我怎样才能得到它们?我目前的解决方案包括获取值并重新绘制图表。它有效,但我对GetChart(Chartname)函数更满意。

我已经看到我可以使用ADODB连接从已关闭的工作簿中获取价值。但与ExecuteExcel4Macro相比,它有点过于复杂。在范围/图表的情况下,使用ADODB连接会更容易吗?

1 个答案:

答案 0 :(得分:4)

以下位代码从已关闭工作簿中的范围中提取信息,并将其复制到活动工作簿中的相同范围内:

    Sub GetRange()
        With Range("A1:D50")                                    'set range to copy from / to.
            .Formula = "='C:\E3_Test\[CC_Data.xlsx]AllData'!A1" 'refers to a workbook, sheet and first cell.
                                                                'It will put the relative references into the target sheet correctly.
            .Value = .Value                                     'changes formula to value.
        End With
    End Sub