err no csv found

instanceoftime

Board Regular
Joined
Mar 23, 2011
Messages
103
I use the code below in a macro. I point to a folder and it merges all csv files within that folder using this code. seems to work perfectly other than I get the error dialog box "no files csv".
It then merges the files in the folder (even though it says there isn't any). I could simply disable the error dialog but would rather fix the problem. Any ideas?

VBA Code:
Sub merge()
'
' merge Macro
' f
'
' Keyboard Shortcut: Ctrl+f
'

'Sub ImportCSVsWithReference()

'UpdatebyKutoolsforExcel20151214

    Dim xSht  As Worksheet

    Dim xWb As Workbook

    Dim xStrPath As String

    Dim xFileDialog As FileDialog

    Dim xFile As String

    On Error GoTo ErrHandler

    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)

    xFileDialog.AllowMultiSelect = False

    xFileDialog.Title = "Select a folder [Kutools for Excel]"

    If xFileDialog.Show = -1 Then

        xStrPath = xFileDialog.SelectedItems(1)

    End If

    If xStrPath = "" Then Exit Sub

    Set xSht = ThisWorkbook.ActiveSheet

    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then xSht.UsedRange.Clear

    Application.ScreenUpdating = False

    xFile = Dir(xStrPath & "\" & "*.csv")

    Do While xFile <> ""

        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)

        Columns(1).Insert xlShiftToRight

        Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name

        ActiveSheet.UsedRange.Copy xSht.Range("A" & Rows.Count).End(xlUp).Offset(1)

        xWb.Close False

        xFile = Dir

    Loop

    Application.ScreenUpdating = True

    Exit Sub

ErrHandler:

    MsgBox "no files csv", , "Kutools for Excel"

End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Step thru your code with F8 and watch the flow to see what it is doing. You can mouse over variables on lines that have been executed to see the values, or use the immediate window to inquire, or open the watch and/or locals vbe window. As much as I can play with that, I can't replicate the issue. However, I did condense your code as it's unnecessarily stretched out so maybe I inadvertently fixed something.
 
Upvote 0
Check this:

VBA Code:
Sub merge()
' merge Macro
' f
' Keyboard Shortcut: Ctrl+f
'Sub ImportCSVsWithReference()
'UpdatebyKutoolsforExcel20151214

    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    
    'On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then xSht.UsedRange.Clear
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.csv")
    If xFile = "" Then
      MsgBox "no files csv", , "Kutools for Excel"
    Else
      Do While xFile <> ""
          Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
          Columns(1).Insert xlShiftToRight
          Columns(1).SpecialCells(xlBlanks).Value = ActiveSheet.Name
          ActiveSheet.UsedRange.Copy xSht.Range("A" & Rows.Count).End(xlUp).Offset(1)
          xWb.Close False
          xFile = Dir
      Loop
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
One reason is that there is (are) *.csv files that have no data
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,102
Members
453,021
Latest member
Justyna P

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