[英]Subscript out of range VBA Excel array
I have this code to eliminate all anomalies in the denormlaized hierarchy table. 我有这段代码可以消除去符号化层次结构表中的所有异常。 When I try to run this macro with a hundred rows of records, maybe around 200 to 300, it works just fine.
当我尝试使用一百行记录(可能大约200到300条记录)运行此宏时,它工作得很好。 But when I try to run the macro with all of my rows, which is around 18,000 lines, it returns the "Subscript out of range" error.
但是,当我尝试对所有行(大约18,000行)运行宏时,它将返回“下标超出范围”错误。 I'm not sure what's wrong with the code 'cause it seems to work fine with hundreds of rows.
我不确定代码有什么问题,因为它似乎可以在数百行中正常工作。 I'm using MS Excel 2010 version.
我正在使用MS Excel 2010版本。 Any help would be appreciated, thank you very much.
任何帮助将不胜感激,非常感谢。
Here's my working code: 这是我的工作代码:
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
Even though you have not mentioned the line where you are getting an error, it is quite obvious. 即使您没有提到出现错误的行,也很明显。 The error is most probably on the line
错误很可能就在网上
arr = Split(arr(1), "]")
And the reason is very simple. 原因很简单。 Because the cell doesn't have "[" so after split there is no
ar(1)
. 因为单元格没有“ [”,所以分割后就没有
ar(1)
。
Here is a very simple way to reproduce the error. 这是重现该错误的非常简单的方法。
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
To ensure that you don't get the error, use INSTR()
to check if [
or ]
exists and then split it. 为确保您没有收到错误,请使用
INSTR()
检查[
或]
存在,然后将其拆分。
For example 例如
If InStr(1, sString, "]") Then
myar = Split(sString, "]")
End If
Followup from comments 评论的跟进
I rewrote your code. 我重写了您的代码。 Is this what you are trying?
这是您要尝试的吗? Please note that I have not tested it so let me know if you get any errors.
请注意,我尚未对其进行测试,因此如果您遇到任何错误,请告诉我。 I have commented the code on the relevant parts as well.
我也对相关部分的代码进行了注释。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.