我在线上收到错误
entries(pos) = oldEntry
在
If newEntry <> oldEntry Then
entries(pos) = oldEntry
oldEntry = newEntry
pos = pos + 1
End If
它说下标超出范围。 &GT;。&LT;
完整代码:
Sub FillItUp()
Dim lastRow As Long, lastCol As Long
Dim oldEntry, newEntry As String
Dim entries(12), toCompare
Dim pos As Integer
lastRow = Sheets(1).Range("A65536").End(xlUp).Row
lastCol = Sheets(1).Range("A6").CurrentRegion.columns.Count
pos = 0
toCompare = Array("Avocent", "Channel", "Enterprise", "Industrial", _
"Service", "Telecom", "Light Industrial")
oldEntry = Sheets(1).Range("E7").Value
entries(pos) = oldEntry
pos = pos + 1
For i = 7 To lastRow
newEntry = Sheets(1).Range("E" & i).Value
If newEntry <> oldEntry Then
entries(pos) = oldEntry
oldEntry = newEntry
pos = pos + 1
End If
If i = lastRow Then
entries(pos) = newEntry
End If
答案 0 :(得分:1)
很难说你是否需要增加数组的初始大小(可能是因为你现在有了比以往更多的可能值),或者你的数据是否错误(可能因为它是要排序但是isn&#t; t)并且(如果它是正确的)在你的阵列中占据少于12个位置。
以下代码将使数组动态化,并在每次找到新值时增加维度。
DefaultController
请注意,您需要更改使用Sub FillItUp()
Dim lastRow As Long, lastCol As Long
Dim oldEntry, newEntry As String
Dim entries(), toCompare
Dim pos As Integer
lastRow = Sheets(1).Range("A65536").End(xlUp).Row
lastCol = Sheets(1).Range("A6").CurrentRegion.columns.Count
pos = 0
ReDim entries(0 To 0)
toCompare = Array("Avocent", "Channel", "Enterprise", "Industrial", _
"Service", "Telecom", "Light Industrial")
oldEntry = Sheets(1).Range("E7").Value
entries(pos) = oldEntry
pos = pos + 1
For i = 7 To lastRow
newEntry = Sheets(1).Range("E" & i).Value
If newEntry <> oldEntry Then
ReDim Preserve entries(0 To pos)
entries(pos) = oldEntry
oldEntry = newEntry
pos = pos + 1
End If
If i = lastRow Then
ReDim Preserve entries(0 To pos)
entries(pos) = newEntry
End If
内容的任何后续代码,以便允许您现在在数组中有entries
个条目,而不是{{ 1}}。
答案 1 :(得分:0)
请在IF语句中检查数组的索引。示例代码如下:
If newEntry <> oldEntry AND (pos >= 0 AND pos < 12) Then
entries(pos) = oldEntry
oldEntry = newEntry
pos = pos + 1
End If