问题:
我认为我根据用户的c
参数输入设置变量Month
的值的方式导致Application-defiend或对象定义的错误。因为当我设置{c = 1的c
值时,程序可以正常工作。如何修复条件语句?
目标:
c=11
c是存储列号的那个,其中c = 1表示列A,11表示列K.我想根据用户的'MONTH'参数输入来设置c的值“BeginDate”= 8/1/2017
问题:
Dim c As Integer
Dim num As Integer
Dim ret(1 To num) As Integer
For num = 1 To num
ret(num) = Month(num) + 3
Next
c = ret(num)
在xlWS.Cells(xlRow, c).Formula = acRng
上给出了应用程序定义或对象定义的错误
完整代码:
Option Compare Database
Option Explicit
Public Function pa()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim acRng As Variant
Dim xlRow As Integer
Dim db As DAO.Database
Dim qry As QueryDef
Dim rst As Recordset
Dim prm As DAO.Parameter
Dim strSQL As String
Set db = CurrentDb
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\J\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Photronics")
xlRow = (xlWS.Columns("K").End(xlDown).Row)
Set qry = db.QueryDefs("15_Photronic-Allen")
qry.Parameters("BeginDate").Value = [Forms]![Run]![textBeginOrderDate]
qry.Parameters("EndDate").Value = [Forms]![Run]![textendorderdate]
Set rst = qry.OpenRecordset(dbOpenDynaset)
Dim c As Integer
Dim num As Integer
Dim ret(1 To 15) As Integer
For num = 1 To num
ret(num) = Month(num) + 3
Next
c = ret(num)
xlRow = xlRow + 2
Do Until rst.EOF
For Each acRng In rst.Fields
xlWS.Cells(xlRow, c).Formula = acRng
c = c + 1
Next acRng
xlRow = xlRow + 1
c = 1
rst.MoveNext
If xlRow > 25 Then GoTo rq_Exit
Loop
rq_Exit:
rst.Close
Set rst = Nothing
Set xlWS = Nothing
xlWB.Close acSaveYes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Function
End Function