I am NOT a programmer - but I need/want to write a command in Excel to aggregate multiple .csv files into separate sheets in one workbook... it runs once, and does copy/paste the contents of one .csv file but then errors out with this error:
Runtime error '438':
Object does not support this property or method.
I've narrowed it down to this line:
'paste it
ThisWorkbook.Worksheets(Sheets.Count).Range("A1").Paste
But, being that I am new to most all of this, I am unsure what to do. So far I have just been grabbing what seemed like applicable code snippets from around the web.
Private Sub CommandButton1_Click()
Dim strFile As String, strPath As String
Dim wkb As Workbook
'Change this path for your own file location:
strPath = "C:[FILE PATH HERE]"
'this returns an empty string "" if the file cannot be found and will error
if the folder is incorrect
strFile = Dir(strPath & "*.csv")
Do While strFile <> ""
'open the csv file and assign it to a variable so that we can easily
reference it later
Set wkb = Workbooks.Open(strPath & strFile)
'add a new worksheet at the end of the macro workbook to paste into
ThisWorkbook.Sheets.Add After:=Sheets(Sheets.Count)
'get the range and copy it
wkb.Sheets(1).UsedRange.Copy
Debug.Print (Sheets.Count)
'paste it
ThisWorkbook.Worksheets(Sheets.Count).Range("A1").Paste
'close the csv file
wkb.Close
'find the next file - Dir without parameters will look for the next file in the folder that matches the first Dir call
strFile = Dir
Loop
End Sub