Macro to pull data from multiple workbooks

MattTonk

New Member
Joined
Mar 13, 2019
Messages
1
Hi everyone - I am trying to create a macro to pull a specific cell from multiple workbooks. This cell will remain the same throughout (A1). The issue I am running into is when the macro is ran it does not immediately identify the sheet. When ran it will force you to select the correct sheet instead of pulling directly from it (Sheet2 instead of Sheet1). All the files are located within the same folder. Any help is appreciated.


Sub GetDataClosedWB()
Dim MyPath As String, MyFile As String
ActiveSheet.UsedRange.Clear
MyPath = ThisWorkbook.Path & Application.PathSeparator
MyFile = Dir(MyPath, vbNormal)
Range("A1").Select
Do While MyFile <> ""
MyShName = Mid(MyFile, 1, Len(MyFile) - 4)
If MyFile <> ThisWorkbook.Name Then
ActiveCell.FormulaR1C1 = "='" & MyPath & "[" & MyFile & "]" & MyShName & "'!R1C1"
ActiveCell.Offset(0, 1).Value = MyFile
ActiveCell.Offset(1, 0).Select
End If
MyFile = Dir
Loop
Columns("A:B").AutoFit


End Sub
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Watch MrExcel Video

Forum statistics

Threads
1,109,493
Messages
5,529,187
Members
409,856
Latest member
MAO
Top