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

lcr*_*rin 2 excel vba adodb excel-vba

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

Arg = "'" & Path & "[" & File & "]" & Sheet & "'!" & "R4C4"  
 Arg = CStr(Arg)
GetValue = ExecuteExcel4Macro(Arg)
Run Code Online (Sandbox Code Playgroud)

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

我对图表有同样的问题,如何获得它们?目前,我的解决方案是获取值并重新绘制图表。它可以工作,但是我对GetChart(Chartname)函数会更满意。

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

小智 5

下面的代码从封闭的工作簿中的某个范围中提取信息,并将其复制到活动工作簿中的相同范围中:

    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
Run Code Online (Sandbox Code Playgroud)