空QueryTable

时间:2018-04-05 18:02:46

标签: vba excel-vba excel

Sub testing()
Dim cn As Object, rs As Object, output As String, sql As String, qt As 
QueryTable, bookSheet As Object, sqlstring As String

Set cn = CreateObject("ADODB.Connection")
With cn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & "I:\Desktop" & "\" & "WebPropertiesSummaryQuery.xlsm" & ";" & _
    "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    .Open
    End With

sql = "SELECT * FROM [Sheet2$] WHERE Rating = 'WBS PWT'"

Set rs = New ADODB.Recordset
rs.Source = sql
rs.ActiveConnection = cn
rs.Open sql

ActiveWorkbook.Sheets(3).Activate
Set bookSheet = Workbooks("WebPropertiesSummaryQuery.xlsm").Worksheets("Sheet3")
Set qt = bookSheet.QueryTables.Add(Connection:=rs, _
Destination:=Range("A1"), sql:=sql)

MsgBox ActiveSheet.QueryTables.Count

rs.Close
cn.Close
Set cn = Nothing
Set rs = Nothing
End Sub

我正在尝试生成一个使用SQL样式查询的表。所有这些都在一个工作簿中完成,其中查询的数据在工作表2中,查询表显示在工作表3上。通过此代码,我可以看到我正在创建一个新的查询表,但表单是空白的。我认为QueryTables.Add行可能存在问题,因为当我添加此代码来测试我的记录集时,它会显示正确的值。

Do
    output = output & rs(0) & ";" & rs(1) & ";" & rs(2) & vbNewLine
    Debug.Print rs(0); ";" & rs(1) & ";" & rs(2)
    rs.Movenext
Loop Until rs.EOF
MsgBox output

1 个答案:

答案 0 :(得分:0)

从sheet2上的以下数据开始:
enter image description here

我在sheet3上得到以下数据:
enter image description here

使用以下代码:

always @(posedge clock) begin