combining csv workbooks to single sheet - vba works but need help adjusting

rikvny02

Board Regular
Joined
Aug 9, 2022
Messages
90
Office Version
  1. 365
Platform
  1. Windows
The below code works exactly as expected. I need help making an adjustment that would not copy the first row in each workbook. Any help is grateful.

Sub CombineCSVSheets()

Dim FolderPath As String
Dim FileName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim wbCombined As Workbook
Dim wsCombined As Worksheet
Dim CurrentRow As Long
Dim rngPaste As Range

'ActiveWorkbook.save

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
FolderName = sItem
Set fldr = Nothing
FolderPath = FolderName & "\"

' Create a new workbook to combine the CSV sheets
On Error Resume Next
Kill (FolderPath & "BATCH 1.xlsx")
On Error GoTo 0
Set wbCombined = Workbooks.Add
With wbCombined
Set wsCombined = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
.SaveAs FolderPath & "BATCH 1.xlsx"
End With

wsCombined.Name = "STATION STAFFING"
Range("a1:k1").Value = Array("TC", "CO", "EMPLOYEE", "POSITION TO", "PT AUTHORIZATION", "FC", "COPY NUMBER", "DATE", "ATTENDANCE", "DESC", "HOURS")

' Retrieve the first CSV file in the folder.
FileName = Dir(FolderPath & "*.csv")

' Loop through all CSV files in the folder
Do While FileName <> ""

With wsCombined
If Len(Trim(.Range("B1"))) = 0 Then
Set rngPaste = .Range("A1")
Else
Set rngPaste = wsCombined.Range("B" & wsCombined.Cells(wsCombined.Rows.Count, 2).End(xlUp).Row + 1).Offset(0, -1)
End If
End With

With wsCombined.QueryTables.Add(Connection:="TEXT;" & FolderPath & FileName, Destination:=rngPaste)
.TextFileParseType = xlDelimited
.TextFileCommaDelimiter = True
.Refresh
End With

' Retrieve the next CSV file in the folder.
FileName = Dir

Loop

' Save and close the combined workbook.
'wbCombined.Close SaveChanges:=TRUE

MsgBox "Finished."

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try adding the line in Blue to your code:

Rich (BB code):
With wsCombined.QueryTables.Add(Connection:="TEXT;" & FolderPath & Filename, Destination:=rngPaste)
    .TextFileParseType = xlDelimited
    .TextFileStartRow = 2
    .TextFileCommaDelimiter = True
    .Refresh
End With
 
Upvote 0
Solution
Try adding the line in Blue to your code:

Rich (BB code):
With wsCombined.QueryTables.Add(Connection:="TEXT;" & FolderPath & Filename, Destination:=rngPaste)
    .TextFileParseType = xlDelimited
    .TextFileStartRow = 2
    .TextFileCommaDelimiter = True
    .Refresh
End With
Thanks you. works great
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,419
Members
452,325
Latest member
BlahQz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top