我想从每个单元格中取出一个字符串,将它拆分成数组,然后决定添加多少个点,然后添加并显示它们。然而,我一直想出一个下标超出范围的错误,我认为它与拆分语句有关,所以我修改了几次,仍然没有得到任何地方。我也认为,也许这不是分裂,也许在那个单元中没有任何东西,但是(ElseIf数组=“”那么)应该照顾到这一点。这里是我的代码:Excel VBA下标超出范围
Sub pointsAdd()
'Init Variables
Dim pointArray() As String
Dim j As Integer
Dim i As Integer
Dim points As Integer
'Make sure the correct sheet is selected
Worksheets("Sheet1").Activate
'Add Points Up
For j = 2 To 100
Cells(j, 1).Select
If ActiveCell.Value = "" Then
j = 100
Else
For i = 3 To 22
Cells(j, i).Select
pointArray = Split(ActiveCell.Value, ".")
'The next line is where the debugger says the script is out of range
If pointArray(0) = "Tardy" Then
points = 0.5
ElseIf pointArray(0) = "Failure To Complete Shift" Then
points = 0.5
ElseIf pointArray(0) = "Failure To Complete At Least Half Shift" Then
points = 0.5
ElseIf pointArray(0) = "Absence" Then
points = 1
ElseIf pointArray(0) = "Late Call Off" Then
points = 2
ElseIf pointArray(0) = "No Call/No Show" Then
points = 4
ElseIf pointArray(0) = "" Then
i = i + 1
Else
MsgBox "Somthing is wrong in Module 1 Points Adding"
End If
'Add points to points cell
Cells(j, 2).Select
points = points + ActiveCell.Value
ActiveCell.Value = points
Next i
End If
Next j
End Sub
而且应该是在单元格中字符串的格式是“Occurrence.Description.Person.mm/dd/yyyy”。
在哪一行你会得到下标超出范围的错误?当出现该错误时,单击调试按钮,导致错误的行将在代码中突出显示。 – NavkarJ
但是你也可以在你的循环中有一个空白的单元格? – SJR
“C:V”列中的单元格是否为空?如果是这样,当你尝试访问'pointArray(0)' – YowE3K