I have wrote this and it works for the most part...for the first file I find. on the second file, I get the following error:
"The information cannot be pasted because the Copy area and the paste area are not the same size and shape. Try one of the following:
- Click a single cell, and then paste.
- Select a rectangle that's the same size and shape, and then paste."
I don't understand what i'm doing wrong.
It is suppose to traverse a directory and grab all the .txt files that are there and import them into either Sheet1 or Sheet2. I can get the first one to import fine, but the next file throws that error instead of appending to the same spreadsheet.
Sub PopulateSheets()
Dim file As String, path As String, fullpath As String, StaticPath As String
Dim count As Integer
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet
Dim Sheet As String
Dim RowCount As Long
On Error GoTo Errorcatch
RowCount = 1
count = 1
StaticPath = Sheet3.Cells(2, 7)
While (count <= 2)
If count = 1 Then
path = StaticPath & "\com\*.txt"
Else
path = StaticPath & "\res\*.txt"
End If
file = Dir(path)
Sheet = "Sheet" & count
While (file <> "")
fullpath = Left(path, InStr(1, path, "*.txt") - 1) & file
Set wbI = ThisWorkbook
Set wsI = wbI.Sheets(Sheet) '<~~ Sheet where I want to import
Set wbO = Workbooks.Open(fullpath)
RowCount = wsI.Range("A:A").CurrentRegion.Rows.count
wbO.Sheets(1).Cells.Copy Destination:=wsI.Range("A" & RowCount)
wbO.Close SaveChanges:=False
file = Dir 'Grab Next File
Wend
count = count + 1
Wend
Exit Sub
Errorcatch:
MsgBox Err.Description
End Sub
It blows up at wbO.Sheets(1).Cells.Copy Destination:=wsI.Range("A" & RowCount)
after it has pasted the information from the first file, closed it, then tries to paste the second file.
Any help would be appreciated at this point.
Side note
I have noticed that if I swapwbO.Sheets(1).Cells.Copy Destination:=wsI.Range("A" & RowCount)
with wbO.Sheets(1).Cells.Copy wsI.Cells
, it will paste all of the files into the sheet...but it overwrites the file before it. I need it to append and not sure how to make that happen.