[英]iterate over database query results in vba in excel
I would like to open a connection to SQL database, and then have access to individual cells. 我想打开一个到SQL数据库的连接,然后可以访问单个单元格。 I have an example that uses PivotTableWizard (presented below).
我有一个使用PivotTableWizard的示例(如下所示)。 I would like to know of a way that does not have anything to do Pivot Tables - I would like to iterate cell-by-cell.
我想知道一种与数据透视表无关的方法-我想逐个单元地迭代。 Or is this PivotTableWizard suitable for that purpose also?
还是此PivotTableWizard也适合该目的?
The example mentioned: 提到的示例:
ConnectionString = "Driver={SQL Server};Server=Serversql11;Persist Security Info=False;Integrated Security=SSPI;Database=DB_IC;"
PivotName = "Talks"
QArray = Array(ConnectionString, _
"exec dbo.talksReport '" & CStr(param_date) & "'")
Worksheets("Talks").PivotTableWizard xlExternal, QArray, Worksheets("Talks").Range("A1"), PivotName
TIA /Karol TIA / Karol
If you want to iterate cell by cell, you could do something like this: 如果要逐个单元地迭代,可以执行以下操作:
Sub PrintCellContentsToDebugWindow()
Dim ws As Worksheet
Dim rng As Range
Dim intCounterRow As Integer
Dim intCounterCol As Integer
Dim intMaxRow As Integer
Dim intMaxCol As Integer
Set ws = ActiveSheet 'OR '
'Set ws = ActiveWorkbook.Sheets("Sheet1") '
intCounterRow = 1
intCounterCol = 1
intMaxRow = 100
intMaxCol = 25
Do While intCounterCol <= intMaxCol
intCounterRow = 1
Do While intCounterRow <= intMaxRow
Set rng = ws.Cells(intCounterRow, intCounterCol)
Debug.Print rng.Address & " " & rng.Value
intCounterRow = intCounterRow + 1
Loop
intCounterCol = intCounterCol + 1
Loop
End Sub
The above code iterates through the first 100 rows and the first 25 columns and prints out the cell address and its value in the Debug Window in the Visual Basic Editor. 上面的代码遍历前100行和前25列,并在Visual Basic编辑器的“调试”窗口中打印出单元地址及其值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.