我想将数据库中所有带有记录的开头为“ 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
我在行上遇到运行时错误“ 1004”:
Set r = r.End(xlDown).Offset(2, 0)
答案 0 :(得分: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