VBA: Importing 3 csv files into 3 different worksheet

Ouble

New Member
Joined
Nov 30, 2015
Messages
18
Hi all, my currently code imports 1 csv data into sheet 1 and then I have to manually click the button and select the next csv file and import to sheet 2. Im thinking if it is possible to select all 3 csv data and import them to sheet 1, sheet 2 and sheet 3 all at once?

any help is greatly appreciated.

Code:
Sub Process()
    Dim strCH As String
    Dim WrtRec As Boolean
    Dim s As String
    Dim v As Variant
    Dim RowNo As Long
    Dim ColNo As Long
    Dim Offset As Integer
    
    Dim strFilename As String
    strFilename = Application.GetOpenFilename("All Files (*.), *.")
    
    If strFilename = "" Then Exit Sub
    
    Dim FileNum As Integer
    FileNum = FreeFile()
    Open strFilename For Input As FileNum
    
    
    Dim ws As Worksheet
    Set ws = ActiveWorkbook.ActiveSheet
    
    Do While Not EOF(FileNum)
        Line Input #1, s
        v = Split(s, vbTab)
        If UBound(v) >= 1 Then
            v(0) = Trim(Replace(v(0), Chr(34), ""))
            Select Case Left(v(0), 3)
                Case "Cha"      'Channel
                    strCH = Trim(Replace(v(1), Chr(34), ""))
                    Select Case strCH
                        Case "CH6"
                            Offset = 1  'Column "A"
                        Case "CH14"
                            Offset = 14 'Column "N"
                        Case Else
                            Offset = 0
                    End Select
                    RowNo = 14
                Case "Nu."
                    WrtRec = True
                Case "---"
                    WrtRec = False
            End Select
            
            If WrtRec And (Offset > 0) Then
                For ColNo = 0 To UBound(v)
                    ws.Cells(RowNo, ColNo + Offset) = Trim(Replace(v(ColNo), Chr(34), ""))
                Next ColNo
                RowNo = RowNo + 1
            End If
        End If
    Loop
    
    Set ws = Nothing
    Close (FileNum)
End Sub

Ouble
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I don't think you can pick them all at the same time, but maybe something like this would help.

Code:
    strFilename = Application.GetOpenFilename("All Files (*.), *.")
    If strFilename = "" Then Exit Sub

    strFilename2 = Application.GetOpenFilename("All Files (*.), *.")
    If strFilename2 = "" Then Exit Sub

    strFilename3 = Application.GetOpenFilename("All Files (*.), *.")
    If strFilename3 = "" Then Exit Sub
.....

Ross
 
Upvote 0
Hi Ross, I wished it was that easy but nope. Tried your suggestion. Only the data from the last file was imported and was to sheet 1. Thanks for giving it a go.

Ouble
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,222
Members
448,951
Latest member
jennlynn

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