我在下面的宏中遇到了一些问题。第一部分中的所有内容都可以正常工作。但是,一旦程序到达第二个“with statement”,AKA,它开始格式化Excel工作表,宏只会在每次运行时工作。意思是,当天的第一次运行给了我正确格式化的表格。完善。第二次运行将把我的所有数据都放到工作表中,但是给了我:“运行时错误'1004'方法'''''''''''''''''''''''''''''''''''它突出显示第二个with语句后面的行,或者“LastRow = Range(”I“& Rows.Count).End(xlUp).Row”代码。让我再说一遍,数据仍然存在于工作表中,就像我说的那样,宏总是一直工作到第二个语句。
Public Sub Command0_Click()
Dim XL As Excel.Application, WB As Excel.workbook, WKS As Excel.Worksheet
Dim db As DAO.Database, rec As DAO.Recordset, f As DAO.Field
Dim i As Integer, j As Integer
Dim LastRow As Long
Set XL = New Excel.Application
XL.Visible = True
Set WB = XL.Workbooks.Add
WB.Activate
WB.SaveAs "C:\Users\******\Desktop\L3 PSR.xls"
Set WKS = WB.Worksheets("sheet1")
Set db = CurrentDb()
Set rec = db.OpenRecordset("2_L3PSR")
With rec
.MoveFirst
i = 7
j = 1
For Each f In .Fields
WKS.Cells(i, j).Value = f.Name
j = j + 1
Next f
Do
i = i + 1
j = 1
For Each f In .Fields
WKS.Cells(i, j).Value = f.Value
j = j + 1
Next f
.MoveNext
Loop Until .EOF
End With
With WKS
LastRow = Range("I" & Rows.Count).End(xlUp).Row
Rows("1:6").RowHeight = 15.75
.... (many more formatting things that is not important...)
End With
End Sub