Hi,
I found a couple of different set of codes to import CSV files into Excel, in the same workbook.
Here is one I found that is quite clean, but I'm having a hard time inserting a header into it.
Option Explicit
Sub CSV()
Dim FilesToOpen As Variant
Dim x As Integer
Dim wkbTemp As Workbook
Dim iCtr As Long
Dim sFileName As String
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files, *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If IsArray(FilesToOpen) = False Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
For iCtr = LBound(FilesToOpen) To UBound(FilesToOpen)
Workbooks.OpenText Filename:=FilesToOpen(iCtr), _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=True, _
Comma:=True, Space:=True, Other:=False
Set wkbTemp = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
MsgBox sFileName
wkbTemp.Worksheets(1).Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
wkbTemp.Close savechanges:=False
Next iCtr
ExitHandler:
Application.ScreenUpdating = True
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
Now this does what I like it to do. What I also want is it to insert Headings into each based on a case statement.
I think its something like this to insert a row for the headings.
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
Then this is what I have so far for The case statement:
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
So in Summary I want this VBA Macro to prompt me to select multiple .csv files and then import them into a single workbook. It will insert a row and add customized headers depending on the filename. I intend to run this every week and the filenames will only change by the last 22 numbers (the date).
I also found this code that works, but it doesn't allow me to select them all at once, I would have to run this program one at a time. Any help or direction would be amazing.
This is what I originally Had:
Sub CSV()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim sFileName As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = ","
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Csv Files (*.csv), *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
' Get file title
Set wkbAll = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
MsgBox sFileName
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
' Place code here to check the name of the worksheet and
' put the proper column headers in row 1
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
.Worksheets(x).Range("A1").Select
End With
Selection.EntireRow.Insert shift:=x1Down
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
Then I Altered it see what would happend:
Sub CSV()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim sFileName As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = ","
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Csv Files (*.csv), *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(x).Copy
' Get file title
Set wkbAll = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
' MsgBox sFileName
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
' Place code here to check the name of the worksheet and
' put the proper column headers in row 1
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
' MsgBox sFileName
' Set wkbAll = ActiveWorkbook
' wkbTemp.Sheets(x).Copy
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
.Worksheets(x).Range("A1").Select
End With
Selection.EntireRow.Insert shift:=x1Down
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
The first codes logic is much clearer.
I found a couple of different set of codes to import CSV files into Excel, in the same workbook.
Here is one I found that is quite clean, but I'm having a hard time inserting a header into it.
Option Explicit
Sub CSV()
Dim FilesToOpen As Variant
Dim x As Integer
Dim wkbTemp As Workbook
Dim iCtr As Long
Dim sFileName As String
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Text Files, *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If IsArray(FilesToOpen) = False Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
For iCtr = LBound(FilesToOpen) To UBound(FilesToOpen)
Workbooks.OpenText Filename:=FilesToOpen(iCtr), _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, Tab:=True, Semicolon:=True, _
Comma:=True, Space:=True, Other:=False
Set wkbTemp = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
MsgBox sFileName
wkbTemp.Worksheets(1).Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
wkbTemp.Close savechanges:=False
Next iCtr
ExitHandler:
Application.ScreenUpdating = True
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
Now this does what I like it to do. What I also want is it to insert Headings into each based on a case statement.
I think its something like this to insert a row for the headings.
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
Then this is what I have so far for The case statement:
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
So in Summary I want this VBA Macro to prompt me to select multiple .csv files and then import them into a single workbook. It will insert a row and add customized headers depending on the filename. I intend to run this every week and the filenames will only change by the last 22 numbers (the date).
I also found this code that works, but it doesn't allow me to select them all at once, I would have to run this program one at a time. Any help or direction would be amazing.
This is what I originally Had:
Sub CSV()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim sFileName As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = ","
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Csv Files (*.csv), *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
' Get file title
Set wkbAll = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
MsgBox sFileName
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
' Place code here to check the name of the worksheet and
' put the proper column headers in row 1
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
.Worksheets(x).Range("A1").Select
End With
Selection.EntireRow.Insert shift:=x1Down
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
Then I Altered it see what would happend:
Sub CSV()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
Dim sFileName As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = ","
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Csv Files (*.csv), *.csv", _
MultiSelect:=True, Title:="Text Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(x).Copy
' Get file title
Set wkbAll = ActiveWorkbook
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
' MsgBox sFileName
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
wkbAll.Worksheets(x).Range("A1").Select
Selection.EntireRow.Insert shift:=x1Down
' Place code here to check the name of the worksheet and
' put the proper column headers in row 1
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
sFileName = Left(wkbTemp.Name, (Len(wkbTemp.Name) - 22))
' MsgBox sFileName
' Set wkbAll = ActiveWorkbook
' wkbTemp.Sheets(x).Copy
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=True, Space:=False, _
Other:=True
.Worksheets(x).Range("A1").Select
End With
Selection.EntireRow.Insert shift:=x1Down
Select Case sFileName
Case "Call Counts Report By Day"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Counts Report By Hour"
wkbAll.Worksheets(x).Range("A1") = "Calls"
wkbAll.Worksheets(x).Range("B1") = "Period"
Case "Call Detail Report By Call"
wkbAll.Worksheets(x).Range("A1") = "Call Id"
wkbAll.Worksheets(x).Range("B1") = "Channel Program"
wkbAll.Worksheets(x).Range("C1") = "Status Event"
wkbAll.Worksheets(x).Range("D1") = "Time"
End Select
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
The first codes logic is much clearer.