【问题标题】:Loop through excel spreadsheet using macro使用宏循环遍历excel电子表格
【发布时间】:2016-04-05 14:47:50
【问题描述】:

我正在尝试遍历 excel 单元格以从 sql server 表中获取值。我编写了一个宏,它将从 excel 中获取 jobnumber 并查询 sql server 故事以获取客户详细信息并将其转储到电子表格中。到目前为止,我只能获取单个单元格的数据。如何循环遍历 Excel 单元格以获得多个工作编号。我是 VBA 的新手。谢谢你的帮助。这是我的宏:

Sub ConnectSqlServer()

Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sConnString As String
Dim newrow As String



newrow = Worksheets("Sheet1").Cells(1, "A").Value

' Create the connection string.
sConnString = "Provider=SQLOLEDB;Data Source=0.0.0.0;" & _
              "Initial Catalog=asset;" & _
              "User ID=Temp;Password=test123;"

' Create the Connection and Recordset objects.
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset

' Open the connection and execute.
conn.Open sConnString
Set rs = conn.Execute("SELECT customer FROM job_tab where jobnum2='" & Trim(newrow) & "';")

' Check we have data.
If Not rs.EOF Then
    ' Transfer result.
    Sheets(1).Range("B1").CopyFromRecordset rs
' Close the recordset
    rs.Close
Else
    MsgBox "Error: No records returned.", vbCritical
End If

' Clean up
If CBool(conn.State And adStateOpen) Then conn.Close
Set conn = Nothing
Set rs = Nothing

结束子

【问题讨论】:

    标签: sql-server vba excel macros


    【解决方案1】:

    您可以这样做,使用 IN SQL 语句:

    SELECT customer FROM job_tab WHERE jobnum2 IN ( 'Value1', 'Value2', ... );
    

    因此,您必须遍历表 A 列中的所有客户并更改 SQL 请求以使用上述语句:

    Sub ConnectSqlServer()
    
    Dim conn As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim sConnString As String
    Dim newrow As String
    
    
    'MODIFIED: create the search string for the IN-Statement
    newrow = "("
    For i = 1 To Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).Row
        newrow = newrow & "'" & Trim(Worksheets("Sheet1").Cells(i, "A").value) & "',"
    Next i
    newrow = Left(newrow, Len(newrow) - 1)
    newrow = newrow & ")"
    
    ' Create the connection string.
    sConnString = "Provider=SQLOLEDB;Data Source=0.0.0.0;" & _
                  "Initial Catalog=asset;" & _
                  "User ID=Temp;Password=test123;"
    
    ' Create the Connection and Recordset objects.
    Set conn = New ADODB.Connection
    Set rs = New ADODB.Recordset
    
    ' Open the connection and execute.
    conn.Open sConnString
    'MODIFIED: altered the SQL statement to use the search string with IN
    Set rs = conn.Execute("SELECT customer FROM job_tab where jobnum2 IN " & newrow & "';")
    
    ' Check we have data.
    If Not rs.EOF Then
        ' Transfer result.
        Sheets(1).Range("B1").CopyFromRecordset rs
    ' Close the recordset
        rs.Close
    Else
        MsgBox "Error: No records returned.", vbCritical
    End If
    
    ' Clean up
    If CBool(conn.State And adStateOpen) Then conn.Close
    Set conn = Nothing
    Set rs = Nothing
    End Sub
    

    【讨论】:

    • @lchixgo - 感谢您的回复。我尝试使用您建议的代码。我收到以下错误消息。运行时错误 -2147217900 (80040e14) 自动化错误
    • @lchixgo-我在这一行收到一条错误消息。 Set rs = conn.Execute("SELECT customer FROM job_tab where jobnum2 IN " & newrow & "';")
    • @Ichixgo- 任何帮助表示赞赏。纠结这个问题太久了。谢谢
    • @LearningMacro 您使用的是哪种 SQL 服务器/数据库?该错误似乎是由 SQL 语句引起的。您能否在错误抛出行之前添加以下行并在此处发布 VBA 控制台的输出:Debug.Print "SELECT customer FROM job_tab where jobnum2 IN " & newrow & "';"
    • @Ichixgo- 我使用的是 SQL Server 2000。我注释掉了出现错误的行。我还添加了行“Debug.Print”SELECT customer FROM job_tab where jobnum2 IN“&newrow&“';”-”。这是 VBA 控制台上的错误:运行时错误“3704”:应用程序定义或对象定义错误。
    猜你喜欢
    • 2017-11-15
    • 1970-01-01
    • 2023-03-16
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    相关资源
    最近更新 更多