excelvbaadodbexcel-4.0

ExecuteExcel4Macro to get range/charts from closed workbooks


I use these lines to get values from closed workbooks:

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

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

I have the same question for charts, how can I get them? My solution for the moment consists of 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?


Solution

  • 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