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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16981081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:31:22  来源:igfitidea点击:

ExecuteExcel4Macro to get range/charts from closed workbooks

excelvbaexcel-vbaadodb

提问by lcrmorin

I use these lines to get values from closed workbooks:

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

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

Is there an other way than loop to get values from a range ? The loop solution is working but It would be clearer if I can get range directly with ExecuteExcel4Macro. I've tried to input a range in Arg, but it doesn't work. It returns an error.

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

I have the same question for charts, how can I get them ? My solution for the moment consist in getting values and replotting the charts. It works, but I would be happier with a GetChart(Chartname) function.

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

I've seen that I can use ADODB connection to get value from closed workbooks. But it was a little too complex compared to ExecuteExcel4Macro. Would it be easier to use ADODB connection in the case of range/charts.

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

回答by Skip Intro

The following bit of code pulls info from a range in a closed workbook and copies it in the same ranges in the Active Workbook:

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

    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