【问题标题】:Excel VBA:Type misMatch error in using Index and Match functionsExcel VBA:使用索引和匹配函数时出现类型不匹配错误
【发布时间】: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 的增量

标签: excel indexing match vba


【解决方案1】:

进行了以下更改(在下面的代码中用 ' 标记)

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

【讨论】:

  • 你现在遇到什么错误?您可以将错误的屏幕截图附加到原始消息中吗?
  • @MajidJavanmard 准备好我上面的消息,请添加您收到的错误消息的屏幕截图。 col , rw 的值是多少?
  • 对不起,Rado 先生打扰您了。我不知道如何将屏幕截图发送给您。但我将我的文件样本上传到此链接:s000.tinyupload.com/?file_id=00243748825638974221
  • 只要告诉我,当您滚动出现错误的行时,colrw 的值是多少?
  • for 1/1/2008 col=1 , rw=1, for 1/2/2008 col=1 rw=33
猜你喜欢
  • 2016-11-14
  • 2020-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多