[英]Excel VBA subscript out of range
I am trying to take a string from each cell split it into the array and then decide how many points to add then add them and display them. 我试图从每个单元格中获取一个字符串,将其拆分为数组,然后确定要添加的点数,然后添加并显示它们。 However I keep coming up with a subscript out of range error I thought it had something to do with the split statement so I revised it several times and still didn't get any where.
但是我一直想出一个下标超出范围的错误,我认为这与split语句有关,因此我对其进行了多次修改,但仍然没有任何意义。 I also then thought maybe it wasn't the split and maybe there was nothing in that cell but with the (ElseIf array = "" Then) should have taken care of that.
然后,我还认为可能不是拆分,并且该单元格中什么也没有,但是使用(ElseIf array =“”然后)应该已经解决了。 Here's my code:
这是我的代码:
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
Also the format of the string that should be in the cell is "Occurrence.Description.Person.mm/dd/yyyy". 单元格中应包含的字符串格式也为“ Occurrence.Description.Person.mm/dd/yyyy”。
You are getting a subscript out of range error whenever your inner for loop gets an empty cell. 每当您的内部for循环获取一个空单元格时,您将得到一个下标超出范围错误。 The following code is a working version of your code above:
以下代码是上述代码的有效版本:
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
Dim Val As String
Val = ActiveCell.Value
' Check if cell value is not empty
If (Val <> "") Then
pointArray = Split(ActiveCell.Value, ".", -1)
'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
Else
' A cell was found empty
i = 23
End If
Next i
End If
Next j
End Sub
Note: It stops to look further when it finds any empty cell in a row. 注意:当它在一行中找到任何空单元格时,将停止进一步查找。 It continues to the next row in that case.
在这种情况下,它将继续到下一行。
You could try this approach, which includes a little tidying up by removing select statements. 您可以尝试这种方法,其中包括通过删除选择语句来进行一些整理。
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
If Cells(j, 1).Value = "" Then
exit for
Else
For i = 3 To 22
pointArray = Split(Cells(j, i).Value, ".", -1)
'The next line is where the debugger says the script is out of range
If UBound(pointArray) > -1 Then
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
End If
'Add points to points cell
points = points + Cells(j, 2).Value
Cells(j, 2).Value = points
Next i
End If
Next j
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.