Try this code out and make sure to change the path in the appropriate line noted.
Sub ABC()
Dim sPath As String, sName As String
Dim bk As Workbook, r As Range
Dim wb As Workbook
Set wb = Workbooks("Master.xls")
Dim sh As Worksheet
Application.ScreenUpdating = False
Set sh = Sheets("Sheet1")
'Change the next line to reflect the path on your computer.
sPath = "C:\Documents and Settings\Alan M Sidman\Desktop\Test\"
sName = Dir(sPath & "*.xls")
Do While sName <> ""
Set bk = Workbooks.Open(sPath & sName)
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Set r = Range("A2:B" & lr)
Dim lrM As Long
wb.Activate
lrM = Range("B" & Rows.Count).End(xlUp).Row
r.Copy wb.Sheets("Sheet1").Range("B" & lrM + 1)
wb.Sheets("Sheet1").Range("A" & lrM + 1) = Left(sName, Len(sName) - 4)
bk.Close SaveChanges:=False
sName = Dir()
Loop
Application.CutCopyMode = xlCopy
Application.ScreenUpdating = True
MsgBox ("Completed")
End Sub
How to install your new code
- Copy the Excel VBA code
- Select the workbook in which you want to store the Excel VBA code
- Press Alt+F11 to open the Visual Basic Editor
- Choose Insert > Module
- Edit > Paste the macro into the module that appeared
- Close the VBEditor
- Save your workbook (Excel 2007+ select a macro-enabled file format, like *.xlsm)
To run the Excel VBA code:- Press Alt-F8 to open the macro list
- Select a macro in the list
- Click the Run button
Bookmarks