Home » excel » excel vba – How to select all .csv files within a folder and then import them through VBA coding

excel vba – How to select all .csv files within a folder and then import them through VBA coding

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am new to VBA and I’m trying to automate a procedure that imports 4 .csv files in a specific folder to the SQL database.
I have a code in which I have 4 command buttons and in each 4, I am picking the files and then importing them to SQL. However, I wanted to know if there is a way in which I can have just 1 command button that picks all the .csv files from that specific folder and then imports them to SQL. Assume the folder name is Database in the following path: Y:\Data\Database

I have the following import button code that converts the local path to SQL server path and all other sorts of functions to import the data to SQL server. (fName1…fName4 are the TextBox values for the 4 file paths currently being picked by the user)

Private Sub ImportButton_Click()
Dim fName1 As String
Dim fName2 As String
Dim fName3 As String
Dim fName4 As String
Dim perc As Single

Dim index As Integer
Dim subStr As String
Dim sqlStr As String
sqlStr = "E:\Analytics\"

'Convert the local path to SQL server path
fName1 = TextBox1.Value
index = InStr(1, fName1, "\")
subStr = Left(fName1, index)
fName1 = Replace(fName1, subStr, sqlStr, , 1)

fName2 = TextBox2.Value
index = InStr(1, fName2, "\")
subStr = Left(fName2, index)
fName2 = Replace(fName2, subStr, sqlStr, , 1)

fName3 = TextBox3.Value
index = InStr(1, fName3, "\")
subStr = Left(fName3, index)
fName3 = Replace(fName3, subStr, sqlStr, , 1)

fName4 = TextBox4.Value
index = InStr(1, fName4, "\")
subStr = Left(fName4, index)
fName4 = Replace(fName4, subStr, sqlStr, , 1)

'Modify the text captions for test purpose
TextBox1.Value = fName1
TextBox2.Value = fName2
TextBox3.Value = fName3
TextBox4.Value = fName4

Dim cnPubs As ADODB.Connection
Dim cmd As ADODB.Command

' Create a connection object.
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Use the SQL Server OLE DB Provider.
strConn = "PROVIDER=SQLOLEDB;"

'Connect to the Pubs database on server hcdcd-actstat01 .
strConn = strConn & "DATA SOURCE=hcdcd-actstat01;INITIAL CATALOG=Analytics;"

'Use an integrated login.
strConn = strConn & " INTEGRATED SECURITY=sspi;"

'Now open the connection.
cnPubs.Open strConn


Set cmd = New ADODB.Command
cmd.ActiveConnection = cnPubs
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "dbo.Proc_CapitalAllocation_Step1"
cmd.CommandTimeout = 1200 'Seconds

''cmd.Parameters.Append cmd.CreateParameter_(fName1, fName2, fName3, fName4)
Call cmd.Execute(Parameters:=Array(fName1, fName2, fName3, fName4), Options:=adCmdStoredProc)

End Sub

Any help would be appreciated.
Thank you so much.

How to&Answers:

This will help you loop through the .csv files in a folder.

Option Explicit

Sub LoopThroughFiles()

    Dim strFile As String, strPath As String

    strPath = "E:\Analytics\"
    strFile = Dir(strPath & "*.csv")

    While strFile <> ""

        '-> code upload the file to SQL Database

        strFile = Dir

    Wend


End Sub

If you need more refinement, like you only want certain .csv files in the folder, add a statement after While strFile <> "" to weed out any files you don’t want. Something like:

If InStr(1, strFile, "myName") > 0 Then
   '-> code to upload the file to SQL database
End If

Answer:

I use the

SearchFile = Dir(SearchFolder & "*.csv")

line to successfully select only .csv files the first time through the code but as I loop through all the files in the folder using

SearchFile = Dir

in each loop, it selects other file types and I haven’t found a way to force that line of code to select only .csv. So my fix was to add an IF statement right after to check it

     SearchFile = Dir         
CheckFile:
     If Right(SearchFile, 3) <> "csv" Then
         SearchFile = Dir
         GoTo CheckFile
     End If

this way, if there is other file types in the folder it just skips over them. I use that format because of other code in my sub but it could be re-written as

CheckFile:
     SearchFile = Dir
     If Right(SearchFile, 3) <> "csv" Then
         GoTo CheckFile
     End If