下标超出范围VBA Excel数组

时间:2014-01-09 06:20:32

标签: arrays excel excel-vba vba

我有这个代码来消除denormlaized层次表中的所有异常。当我尝试使用一百行记录运行此宏时,可能大约200到300,它可以正常工作。但是当我尝试使用我的所有行(大约18,000行)运行宏时,它会返回“下标超出范围”错误。我不确定代码有什么问题,因为它似乎可以正常运行数百行。我正在使用MS Excel 2010版本。任何帮助将不胜感激,非常感谢。

这是我的工作代码:

    Option Explicit

Sub EliminateAnomaliesDH()
Sheets("Denorm Hier").Select
Range("A1").Select
Dim iCtr As Integer
Dim arr As Variant

iCtr = 2

While Range("B" & iCtr).Value <> ""
arr = Split(Range("B" & iCtr).Value, "[")
arr = Split(arr(1), "]")

Select Case arr(0)
    Case "L1"
        Range("F" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L2"
        Range("H" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L3"
        Range("J" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L4"
        Range("L" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L5"
        Range("N" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L6"
        Range("P" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L7"
        Range("R" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L8"
        Range("T" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L9"
        Range("V" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L10"
        Range("X" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L11"
        Range("Z" & iCtr & ":AB" & iCtr & "").Value = ""
    Case "L12"
        Range("AB" & iCtr & ":AB" & iCtr & "").Value = ""
End Select

iCtr = iCtr + 1
Wend

Sheets("Instructions").Select
MsgBox "Successfully removed all anomalies of the Denormalized hierarchy Table"
End Sub

1 个答案:

答案 0 :(得分:1)

即使您没有提到出现错误的行,但这很明显。错误最有可能在线

arr = Split(arr(1), "]")

原因很简单。因为单元格在分割后没有“[”所以没有ar(1)

这是一种重现错误的简单方法。

Sub sample()
    Dim sString As String
    Dim myar

    sString = "Blah Blah"

    myar = Split(sString, "]")

    myar = Split(myar(1), "[") '<~~ Error here

    Debug.Print myar(0)
End Sub

为确保您不会收到错误,请使用INSTR()检查[]是否存在,然后将其拆分。

例如

    If InStr(1, sString, "]") Then
        myar = Split(sString, "]")
    End If

通过评论进行跟进

我重写了你的代码。这是你在尝试什么?请注意,我没有测试过,所以如果您有任何错误,请告诉我。我也对相关部分的代码进行了评论。

Sub EliminateAnomaliesDH()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long
    Dim tempString As String, sString As String

    Set ws = ThisWorkbook.Sheets("Denorm Hier")

    With ws
        '~~> Get the last row which has data in Col B
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row

        '~~> Loop through cells in column B
        For i = 2 To lRow
            sString = .Range("B" & i).Value

            '~~> Check if the cell has both "[" and "]"
            If InStr(1, sString, "[") And InStr(1, sString, "]") Then
                tempString = Split(.Range("B" & i).Value, "[")(1)
                tempString = Split(tempString, "]")(0)

                '~~> This required so that we do an exact match
                '~~> For example, "  l1", "  l1   ", "   L1" etc
                '~~> becomes "L1"
                tempString = UCase(Trim(tempString))

                Select Case tempString
                    Case "L1": .Range("F" & i & ":AB" & i & "").ClearContents
                    Case "L2": .Range("H" & i & ":AB" & i & "").ClearContents
                    Case "L3": .Range("J" & i & ":AB" & i & "").ClearContents
                    Case "L4": .Range("L" & i & ":AB" & i & "").ClearContents
                    Case "L5": .Range("N" & i & ":AB" & i & "").ClearContents
                    Case "L6": .Range("P" & i & ":AB" & i & "").ClearContents
                    Case "L7": .Range("R" & i & ":AB" & i & "").ClearContents
                    Case "L8": .Range("T" & i & ":AB" & i & "").ClearContents
                    Case "L9": .Range("V" & i & ":AB" & i & "").ClearContents
                    Case "L10": .Range("X" & i & ":AB" & i & "").ClearContents
                    Case "L11": .Range("Z" & i & ":AB" & i & "").ClearContents
                    Case "L12": .Range("AB" & i & ":AB" & i & "").ClearContents
                End Select
            End If
        Next i
    End With
    MsgBox "Successfully removed all anomalies of the Denormalized hierarchy Table"
End Sub