繁体   English   中英

如何选择文件夹中的所有.csv文件,然后通过VBA编码导入它们

[英]How to select all .csv files within a folder and then import them through VBA coding

我是VBA的新手,我正在尝试自动化将特定文件夹中的4 .csv文件导入SQL数据库的过程。 我有一个代码,其中我有4个命令按钮,每4个,我选择文件,然后将它们导入SQL。 但是,我想知道是否有一种方法可以让我只有一个命令按钮,从该特定文件夹中选择所有.csv文件,然后将它们导入SQL。 假设文件夹名称是以下路径中的数据库:Y:\\ Data \\ Database

我有以下导入按钮代码将本地路径转换为SQL服务器路径和所有其他类型的函数将数据导入SQL服务器。 (fName1 ... fName4是用户当前正在拾取的4个文件路径的TextBox值)

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

任何帮助,将不胜感激。 非常感谢。

这将帮助您遍历文件夹中的.csv文件。

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

如果您需要更多细化,例如您只需要文件夹中的某些.csv文件,请在While strFile <> ""之后添加一个语句来清除您不想要的任何文件。 就像是:

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

我用

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

第一次通过代码成功选择.csv文件的行,但是我循环遍历文件夹中的所有文件

SearchFile = Dir

在每个循环中,它选择其他文件类型,我还没有找到一种方法来强制该行代码只选择.csv。 所以我的修复是在检查后立即添加IF语句

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

这样,如果文件夹中有其他文件类型,它就会跳过它们。 我使用那种格式,因为我的sub中的其他代码,但它可以重写为

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM