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
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