The goal of this Macro is to combine data from multiple excel files from different months into one master excel file. I have created a tab for every month in the master excel file (see attached picture) that I would like the data to go to and stack on top of each other.
I found some helpful code that I got to work a few times but after modifications is now broken. A few issues that I think I need to resolve before it can work like I want are:
This code is built with a fixed range that gets copied. The excel files I am looking at will have ranges that are variable.
The code keeps breaking on the line that reads
Set wbTemp = Workbooks.Open(Filename:=FilePath & MyFile, ReadOnly:=True)
. This could be because I am testing excel files in different month folders all that have the same name?
I am getting the following error: "Run-time error '1004': Microsoft Excel cannot access the file 'S:\Actg\TESTING\September\Loans_20180920.csv'. There are several possible reasons: -The file name or path does not exist. -The file is being used by another program". I went through and deleted all of the other excel files that I was testing except the ones in the September folder but I am still getting this error.
- Is there a way I can modify this code so that I don't have to copy it 12 times for every month? I was thinking it would be nice if a text box was prompted where I entered the month I wanted to download. Either way... I have already copied it 12 times so it wouldn't be any extra work.
Original Code Reference: Dan Wagner (Copying worksheets from multiple workbooks into current workbook)
Here's the code I am working with:
Sub Stack_Overflow_Example()
Dim MyFile As String, MyFiles As String, FilePath As String
Dim erow As Long
Dim wbMaster As Workbook, wbTemp As Workbook
Dim wsMaster As Worksheet, wsTemp As Worksheet
FilePath = "S:\Actg\TESTING\September\"
MyFiles = "S:\Actg\TESTING\September\*.csv"
MyFile = Dir(MyFiles)
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
Set wbMaster = ThisWorkbook
Set wsMaster = wbMaster.Sheets("September")
Do While Len(MyFile) > 0
If MyFile <> "master.xlsm" Then
Set wbTemp = Workbooks.Open(Filename:=FilePath & MyFile, ReadOnly:=True)
Set wsTemp = wbTemp.Sheets(1)
With wsMaster
erow = .Range("A" & .Rows.Count).End(xlUp).Row
wsTemp.Range("A2:U88").Copy
.Range("A" & erow).Offset(1, 0).PasteSpecial xlPasteValues
End With
wbTemp.Close False
Set wsTemp = Nothing
Set wbTemp = Nothing
End If
MyFile = Dir
Loop
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Please let me know if you have additional questions.