Error when importing csv file

szeconku

New Member
Joined
Jul 31, 2015
Messages
2
I have a macro that imports csv-files into sheets with the same name in a workbook. All the csv files end with ".csv" except for one file which ends with ".CSV". The macro is importing the csv files that end with ".csv" fine. But when it encounters the csv file with ".CSV" it adds a new sheet. I think it's a matter of deactiviting the case sensivity (and I've tried) but I'm not sure. Here's the code:

Code:
Private Sub importOrUpdate(opr$)
    Dim csvFile, csvArr
    Dim wsCSV As Worksheet, wsImport As Worksheet
    Dim importFolder$, cnt%, i%
    Dim csvName$, idx%, arr, shName$
    Dim processed$

U.Start
processed = "|"

csvArr = selectFiles

For i = 0 To UBound(csvArr)
    'Workbooks.Open csvArr(i), False, True
    Call importToTempSheet(csvArr(i))
    Set wsCSV = Tempsheet

    idx = InStrRev(csvArr(i), "\") + 1
    csvName = Mid(csvArr(i), idx)
    csvName = Replace(csvName, ".csv", "")
    arr = Split(csvName, "_")

    If UBound(arr) = 2 Then
        shName = arr(1) & "_" & arr(2)
    Else
        shName = csvName
    End If

    On Error Resume Next
    Set wsImport = ThisWorkbook.Sheets(shName)
    On Error GoTo 0

    If wsImport Is Nothing Then
        ThisWorkbook.Sheets.Add before:=Sheet14
        Set wsImport = ActiveSheet
        wsImport.Tab.Color = 5296274
        wsImport.Name = shName
        Call import(wsCSV, wsImport)
    ElseIf opr = "Update" Then
        Call update(wsCSV, wsImport)
    ElseIf InStr(1, processed, "|" & shName & "|", vbTextCompare) > 0 Then
        Call update(wsCSV, wsImport)
    Else
        Call import(wsCSV, wsImport)
    End If

    Call updateFormula(wsImport)
    processed = processed & shName & "|"
    cnt = cnt + 1
    'wsCSV.Parent.Close False
Next

Sheet14.Activate
U.Finish

MsgBox cnt & " files imported/updated", vbInformation

End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub importToTempSheet(filePath)
Dim lRow&

Tempsheet.Cells.Clear

Dim wsCSV As Worksheet
Workbooks.Open filePath, False, True
Set wsCSV = ActiveWorkbook.Sheets(1)

lRow = wsCSV.Cells(Rows.Count, "A").End(xlUp).Row

wsCSV.Range("A1:A" & lRow).Copy
Tempsheet.Range("A1").PasteSpecial xlPasteValues
Application.CutCopyMode = False
wsCSV.Parent.Close

Tempsheet.Range("A1:A" & lRow).TextToColumns Tempsheet.Range("A1"), xlDelimited, xlTextQualifierNone, False, False, True, False, False

With Tempsheet
    .Range("A:A").NumberFormat = "m/d/yyyy"
    convertToDate .Range("A2", .Cells(Rows.Count, "A").End(xlUp))
End With
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Function selectFiles()
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select CSV Files"
        .ButtonName = "Select"
        .AllowMultiSelect = True
        .Filters.Clear
        .Filters.Add "Excel Files", "*.csv"
        .InitialFileName = ThisWorkbook.Path & "\"
        .Show

    If .SelectedItems.Count = 0 Then
        End
    Else
        Dim csvArr, i%
        ReDim csvArr(.SelectedItems.Count - 1)
        For i = 1 To .SelectedItems.Count
            csvArr(i - 1) = .SelectedItems(i)
        Next
        selectFiles = csvArr
    End If
End With
End Function
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Change this:
Code:
csvName = Replace(csvName, ".csv", "")
to this:
Code:
csvName = Replace(Expression:=csvName, Find:=".csv", Replace:="", Compare:=vbTextCompare)
so it's not case-sensitive.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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