大家好,我一直在努力寻找解决我的错误的方法,但我在网上找不到类似的例子来解决这个问题。我有一个变量(i)在进入循环时设置为 3,然后进入第一个 if 语句它运行良好,文件被复制并插入到工作表 3 但是一旦它增加到 4 并进入第二个 if 语句我得到一个错误 9
Set wks4 = wkb.Sheets(i)
这是下面完整代码的一部分,希望你们能帮助我。
Set Excel_App = New EXCEL.Application
Excel_App.Visible = False
Set wkb = EXCEL.Workbooks.Add
Set wks = wkb.Sheets(1) 'want first sheet
With wks
......
'extra code involing input to sheet 1
End With
Dim wkb2 As EXCEL.Workbook
Set wkb2 = EXCEL.Application.Workbooks.Open(strDir & "\DETAILS.xlsx")
wkb2.ActiveSheet.Cells.Select
Selection.Copy
Set wks2 = wkb.Sheets(2)
With wks2
.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
End With
wkb2.Close
i = 3
Do While Not rstTypes.EOF
If rstTypes![TYPE] Like "GROUPED" Then
DoCmd.OutputTo acOutputReport, "EXCEL_CBD_CAP_CR", acFormatXLS, strDir & "\SAMEDAY_NEXTDAY_FUTURE.xls", 0
Dim wkb3 As EXCEL.Workbook
Set wkb3 = EXCEL.Application.Workbooks.Open(strDir & "\SAMEDAY_NEXTDAY_FUTURE.xls")
wkb3.ActiveSheet.Cells.Select
Selection.Copy
Set wks3 = wkb.Sheets(i)
With wks3
.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
wkb3.Close
i = i + 1
End If
If rstTypes![TYPE] Like "GROUPED2" Then
DoCmd.OutputTo acOutputReport, "EXCEL_ND/FUT_CAP_CR", acFormatXLS, strDir & "\NEXTDAY_FUTURE.xls", 0
Dim wkb4 As EXCEL.Workbook
Set wkb4 = EXCEL.Application.Workbooks.Open(strDir & "\NEXTDAY_FUTURE.xls")
wkb4.ActiveSheet.Cells.Select
Selection.Copy
Set wks4 = wkb.Sheets(i)
With wks4
.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End With
wkb4.Close
Kill (strDir & "\NEXTDAY_FUTURE.xls")
i = i + 1
End If
rstTypes.MoveNext
Loop