Pulling data from another workbook with VBA code.

andrelu78

New Member
Joined
Jun 11, 2018
Messages
1
Hello! I am trying to pull data from another workbooks with the INDIRECT function. I have the following table in the main workbook:

File
Value
test1
B2
test2
B2
test3
B2
test4
B2
test5
B2
test6
B2

<tbody>
</tbody>

I am using this VBA code based on another posted in this forum:

Code:
Sub Open_Workbooks_and_Create_Formulas2()
    Dim r As Long
    
    Application.ScreenUpdating = False
    
    With ThisWorkbook.ActiveSheet
        
        For r = 2 To .Cells(Rows.Count, "A").End(xlUp).Row
            Workbooks.Open ThisWorkbook.Path & "\" & Cells(r, "A") & ".xlsx"
            
            '=INDIRECT("'C:\Users\a278078\Desktop\["&TEXT(A2")&".xlsx]Sheet1'!"&B2)
            
         .Cells(r, "C").ClearContents
         .Cells(r, "C").Formula = "=INDIRECT(""'" & ThisWorkbook.Path & "\[" & "A" & r & ".xlsx]Sheet1'!""&B" & r & ")"
         
        Next
        
        .Activate
        
    End With
            
    Application.ScreenUpdating = True
    
End Sub
Code:

What this does is open the files listed in the collum A and uses the INDIRECT function to pull data specified in the collum B to collum C.

The problem is that when trying to open the secound file, the code is reading the cell in the first file that was opened, not the main workbook.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Forum statistics

Threads
1,214,920
Messages
6,122,267
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top