当日期计数器发生变化时,我在索引和匹配函数中遇到错误。当我面对错误时,我写了一条评论。这是代码:
Sub regionalAverage()
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Dim address(2) As String
Dim rw As Variant
Dim col As Variant
Dim date_ini As Date
Dim date_fin As Date
'create WorkSheet
date_ini = #1/1/2008#
date_fin = #1/4/2008#
For j = 1 To 3
For conteo = date_ini To date_fin
For i = 1 To 2
With Sheets(i)
With Application
col = .Match(j, Worksheets(i).Range("F2:F23393"), 0)
rw = .Match(CLng(conteo), Worksheets(i).Range("D2:D23393"), 0)
address(i) = .Index(Worksheets(i).Range("H2:H23393"), col, rw)
' the error appear here
End With
End With
Next i
' computation
area = 6.429571
Sheets("Output").Activate
Range("A1").Select
ActiveCell.Offset(0, j).Select
colname = Split(ActiveCell(1).address(1, 0), "$")(0)
Columns("" & colname & ":" & colname & "").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select
ActiveCell.Value = "=(SUM(" & address(1) & "," & address(2) & "))/" & area & ""
Next conteo
Next j
End Sub
当日期变为1/2/2008时,我面临错误,我该如何解决?
谢谢
答案 0 :(得分:0)
进行了以下更改(在下面的代码中标有'
)Sub regionalAverage()
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
' *** change the declaration here ***
Dim address() As String
Dim rw As Variant
Dim col As Variant
Dim date_ini As Date
Dim date_fin As Date
' *** add Redim here, so the index of the array will start from 1 ***
ReDim address(1 To 2)
date_ini = #1/1/2008#
date_fin = #1/4/2008#
For j = 1 To 3
For conteo = date_ini To date_fin
For i = 1 To 2
With Sheets(i)
With Application
col = .Match(j, Worksheets(i).Range("F2:F23393"), 0)
rw = .Match(CLng(conteo), Worksheets(i).Range("D2:D23393"), 0)
On Error Resume Next
address(i) = .Index(Worksheets(i).Range("H2:H23393"), rw, col) ' switched between rw (i think it's your row reference) and col
' to help debug the error you get
If Err.Number <> 0 Then
MsgBox "Error number " & Err.Number & " in row " & rw & " Col " & col
End If
On Error GoTo 0
End With
End With
Next i
' computation
area = 6.429571
Sheets("Output").Activate
Range("A1").Select
ActiveCell.Offset(0, j).Select
colname = Split(ActiveCell(1).address(1, 0), "$")(0)
Columns("" & colname & ":" & colname & "").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Select
ActiveCell.Value = "=(SUM(" & address(1) & "," & address(2) & "))/" & area & ""
Next conteo
Next j
End Sub