【发布时间】:2016-07-12 08:32:53
【问题描述】:
当日期计数器更改时,我在索引和匹配函数中遇到错误。当我遇到错误时,我写了一条评论。这是代码:
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
当日期更改为 2008 年 1 月 2 日时,我遇到了错误,我该如何解决?!
谢谢
【问题讨论】:
-
您是指
address(i) = .Index的那一行吗?或者在它之后,End With的行? -
我的意思是
address(i) = .Index的那一行 -
你声明了
Dim address(2) As String,当 i = 2 时,你已经在查看数组成员 3,并且你得到了错误(address() 数组从 0 开始,然后是 1)。 -
你的意思是
dim address(number) as string中的数字应该大于计数器i? -
当你声明数组时,你从 0 开始(除非你使用 Redim)。并且您的 For i 循环从 1 开始,因此计数器中有 1 的增量