问题
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.
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.
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.
回答1:
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
来源:https://stackoverflow.com/questions/16981081/executeexcel4macro-to-get-range-charts-from-closed-workbooks