【问题标题】:VBA excel macroVBA excel宏
【发布时间】:2011-01-14 09:56:45
【问题描述】:

我在 Excel 中有这个 VBA 宏。当用户单击工作表上的按钮时,宏会将结果返回到工作表。 我想问的是,如何使用下面的代码在同一张表中运行多个查询(返回不同的结果)?

Sub Stats2()
  Workbooks("macro.xls").Sheets("Sheet3").Select
  ActiveSheet.Range("A1").Select

  Dim objConn As ADODB.Connection
  Dim rsData As ADODB.Recordset
  Dim strSQL As String
  szconnect = "Provider=SQLOLEDB;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=*******88;Data Source=****"

  ''#Create the Connection and Recordset objects.
  Set objConn = New ADODB.Connection
  Set rsData = New ADODB.Recordset

  On Error GoTo errHandler

  ''#Open the Connection and execute the stored procedure
  objConn.Open szconnect
  strSQL = "select * from CATEGORY_TYPE  "
  objConn.CommandTimeout = 0
  Set rsData = objConn.Execute(strSQL)

  For iCols = 0 To rsData.Fields.Count - 1
    ActiveSheet.Range("A3").Select
    ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column + iCols).Value = rsData.Fields(iCols).Name
    ActiveSheet.Cells.Font.Name = "Arial"
    ActiveSheet.Cells.Font.Size = 8
    ActiveSheet.Cells.EntireColumn.AutoFit
  Next

  ActiveSheet.Range(ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column), ActiveSheet.Cells(ActiveCell.Row, ActiveCell.Column + rsData.Fields.Count)).Font.Bold = True
  j = 2

  If Not rsData.EOF Then
    ''#Dump the contents of the recordset onto the worksheet
    On Error GoTo errHandler
    ActiveSheet.Cells(ActiveCell.Row + 1, ActiveCell.Column).CopyFromRecordset rsData
    If Not rsData.EOF Then
      MsgBox "Data set too large for a worksheet!"
    End If
    rsData.Close 
  End If

  Unload frmSQLQueryADO
  Exit Sub

  errHandler:
    MsgBox Err.Description, vbCritical, "Error No: " & Err.Number
    ''#Unload frmSQLQueryADO
End Sub

【问题讨论】:

  • 你可以从一个方法调用多个方法,每个方法执行它自己的逻辑。我不确定我是否理解这个问题。
  • 我的意思是在这段代码中 strSQL = "select * from ACCOUNT_CATEGORY_TYPE" 我如何添加另一个查询?我可以像这样添加它 strSQL = "select * from ACCOUNT_CATEGORY_TYPE "+ "select * from users" 吗?
  • 你需要澄清问题。为什么要运行多个查询?您是否意识到评论中示例的返回集中没有逻辑?
  • refined...我想运行多重查询的原因是因为这些查询返回不同的结果。

标签: excel vba


【解决方案1】:

您应该从上级程序中准确调用此过程,将查询和起始单元格作为参数传递,然后您可以按照您的需要多次运行它来填充工作表。

小例子:

sub stats2()
    dim lastrow as string
    lastrow = "A1"
    lastrow = Query1(lastrow,"select * from foo")
    lastrow = Query1(lastrow,"select * from other")
end sub

sub query1(startingrow as string, sqlquery as string) as string
    'your code here. Take in mind that you can have a connection opened outside of here
    'The rest could be the same
    'just use the two parameters, one for the query, the other for the range you 
    'start filling the columns name.
    [code here]
    'return the las used row.
end sub

【讨论】:

  • ermmm 好的,你能给我一个关于如何做到这一点的例子吗..这里不是真正的 VB 程序员..
【解决方案2】:

如果您可以在 SQL 服务器上的存储过程中设置查询,则查询将被执行并返回单独的结果集,例如:

   Dim rsData As ADODB.Recordset
    Dim oCmd As ADODB.Command

    With oCmd
        .ActiveConnection = objConn
        .CommandType = adCmdStoredProc
        .CommandText = "SprocName"
    End With

    Set rsData = oCmd.Execute()

    'do stuff with the first results

    rsData.NextRecordset
    'rsData will now be the next set of results

    rsData.NextRecordset
    'rsData will now be the third set of results 

您也可以指定多个查询

.CommandType = adCmdText
.CommandText = "Select * FROM Foo; Select * FROM Bar;"

拥有 ;在每个查询之间,我不确定这个,我通常设置一个sproc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-10
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多