简体   繁体   English

将具有结果的多个查询导出到Excel到一个工作表中

[英]Export multiple queries with results to Excel into one worksheet

I want to export all queries in my database with the beginning "WWEI", which have records, into one Excel worksheet and list them under each other. 我想将数据库中所有带有记录的开头为“ WWEI”的查询导出到一个Excel工作表中,并将它们相互列出。

strFullPath = "C:\Users\test.xlsx"
Set wb = xl.Workbooks.Add
Set wb = xl.Workbooks.Open(strFullPath)   
Set ws = wb.Worksheets(1)      
Set r = ws.Range("a1")         
r = "Possible Mistakes"

Set r = r.Offset(2, 1)

For Each qdf In CurrentDb.QueryDefs
    If Mid(qdf.Name, 1, 4) = "WWEI" Then
        querybezeichnung = qdf.Name
        If DCount("*", querybezeichnung) > 0 Then

            Set rs = CurrentDb.OpenRecordset(querybezeichnung)
            With rs
                For i = 1 To .Fields.Count
                    r.Cells(1, i) = .Fields(i - 1).Name
                    r.Cells(1, i).Font.Bold = True
                    'r.Cells(1, i).AutoFilter
                Next i
            End With
            Set r = r.Offset(1, 0)

            r.CopyFromRecordset rs
            rs.Close
            Set r = r.End(xlDown).Offset(2, 0)

        End If
    End If
Next qdf

I have a runtime error "1004" on line: 我在行上遇到运行时错误“ 1004”:

Set r = r.End(xlDown).Offset(2, 0)
Set r = r.End(xlDown).Offset(2, 0)
 instead of dis can't you use
Set r = r.Offset( recordcountofrs + 2, 0)
'________________________________________________________
strFullPath = "C:\Users\test.xlsx"
Set wb = xl.Workbooks.Add
Set wb = xl.Workbooks.Open(strFullPath)   
Set ws = wb.Worksheets(1)      
Set r = ws.Range("a1")         
r = "Possible Mistakes"

Set r = r.Offset(2, 1)

For Each qdf In CurrentDb.QueryDefs
    If Mid(qdf.Name, 1, 4) = "WWEI" Then
        querybezeichnung = qdf.Name
        If DCount("*", querybezeichnung) > 0 Then

            Set rs = CurrentDb.OpenRecordset(querybezeichnung)
            With rs
                For i = 1 To .Fields.Count
                    r.Cells(1, i) = .Fields(i - 1).Name
                    r.Cells(1, i).Font.Bold = True
                    'r.Cells(1, i).AutoFilter
                Next i
            End With
            Set r = r.Offset(1, 0)

            r.CopyFromRecordset rs
        LstRow = rs.RecordCount '<== this line added 
            rs.Close
            Set r = r.Offset(LstRow+2, 0) '<== this line changed 

        End If
    End If
Next qdf

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

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