我想從每個單元格中取出一個字符串,將它拆分成數組,然後決定添加多少個點,然後添加並顯示它們。然而,我一直想出一個下標超出範圍的錯誤,我認爲它與拆分語句有關,所以我修改了幾次,仍然沒有得到任何地方。我也認爲,也許這不是分裂,也許在那個單元中沒有任何東西,但是(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