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:

[TABLE="width: 500"]
<tbody>[TR]
[TD]File
[/TD]
[TD][/TD]
[TD]Value
[/TD]
[/TR]
[TR]
[TD]test1
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]test2
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]test3
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]test4
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]test5
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]test6
[/TD]
[TD]B2
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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