Excel 将序列号分配到第一列中的特定范围
我正在尝试向我的项目添加一个vba部分,该部分将序列号分配给第一列的彩色范围,但是没有成功。我想出了以下代码:Excel 将序列号分配到第一列中的特定范围,excel,vba,for-loop,Excel,Vba,For Loop,我正在尝试向我的项目添加一个vba部分,该部分将序列号分配给第一列的彩色范围,但是没有成功。我想出了以下代码: Private Sub CommandButton1_Click() On Error GoTo ErrorHandler Dim serial, i, EndRow, StartRow As Integer Dim row As Range, cell As Range 'Discover the data starting an
Private Sub CommandButton1_Click()
On Error GoTo ErrorHandler
Dim serial, i, EndRow, StartRow As Integer
Dim row As Range, cell As Range
'Discover the data starting and end rows
i = 1
serial = 1
StartRow = 1
EndRow = 1
'Check the first cell of each row for the data-start background colour
For Each row In ActiveSheet.UsedRange.Rows
Cells(row.row, 1).Select
If i < 3 Then
If Hex(cell.Interior.Color) = "47AD70" And i = 1 Then
Cells(row.row, 1).Value = Abs(serial)
StartRow = serial
serial = serial + 1
i = 2
ElseIf Hex(cell.Interior.Color) = "47AD70" And iRow = 2 Then
Cells(row.row, 1).Value = Abs(serial)
serial = serial + 1
ElseIf Hex(cell.Interior.Color) <> "47AD70" And iRow = 2 Then
EndRow = serial - 1
i = 3
End If
End If
Next row
ErrorHandler:
If Err.Number <> 0 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & "Error Line: " & Erl & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
End If
End Sub
变量i用作检测彩色范围的标志
最后,绿色单元格A5至A22应填充数字1至18。此外,变量StartRow最终应被指定为有色范围起始行的值=5,而EndRow最终应被指定为有色范围结束行的值=22
我的代码生成错误91,对象变量或未设置块变量
除了我无法纠正的错误之外,我知道代码本身也没有那么聪明,可能有更有效的代码来实现目标
有人能提出一个解决方案或者更好的代码吗?
非常感谢
请更换
与
仅选择一个单元格,不会告诉VBA这就是代码所需的单元格:
您有一个输入错误:
它显然应该是:
And i = 2 Then
这就是使用EndRow变量的目的。如果它不是用来做某事的。它只在最后接收一个值
只有在第一行代码中的下一行输入Error Resume时,才能使用/On Error GoTo ErrorHandler。否则,将不会捕获任何错误
然后,不建议使用行、单元格等变量名。它们可能会在调试复杂代码时产生问题…我可能不完全理解您试图做什么。下面的代码将查找A列中的第一个绿色单元格,给该单元格序列号1,并继续对后续绿色单元格进行序列编号。我希望这就是你想要的
Private Sub CommandButton1_Click()
Const Col As Long = 4697456 ' = &H47AD70
Dim Ws As Worksheet
Dim Rcount As Long ' Row
Dim R As Long
Set Ws = ActiveSheet ' better = Worksheets("Sheet1")
With Ws
Rcount = .UsedRange.Rows.Count
For R = 2 To Rcount
' find the frist occurrence of Col
If .Cells(R, "A").Interior.Color = Col Then Exit For
Next R
If R > Rcount Then
MsgBox "No cells of the specified colour were found.", _
vbInformation, "Unuccessful search"
Else
Rcount = R - 1
Do
.Cells(R, "A").Value = R - Rcount
R = R + 1
' loop until a different cell colour is encountered
Loop While .Cells(R, "A").Interior.Color = Col
End If
End With
End Sub
请注意,在ActiveSheet上进行更改通常是危险的。危险在于用户在错误的工作表处于活动状态时错误地运行代码。因此,最好修改上述代码,按名称指定所需的工作表。谢谢。第一个答案中的建议解决了这个问题。然而,为了进一步澄清,我希望A列中的绿色单元格最终具有序列号1到18;或者绿色细胞在柱中持续多长时间;最后,变量StartRow和EndRow具有列A中第一个和最后一个绿色单元格的行号值。该代码在为StartRow和EndRow分配正确的行号方面存在更多问题。分配行号的行应该是:StartRow=row.row和EndRow=row.row-1,这样它就可以正常工作。
And iRow = 2 Then
And i = 2 Then
Private Sub CommandButton1_Click()
Const Col As Long = 4697456 ' = &H47AD70
Dim Ws As Worksheet
Dim Rcount As Long ' Row
Dim R As Long
Set Ws = ActiveSheet ' better = Worksheets("Sheet1")
With Ws
Rcount = .UsedRange.Rows.Count
For R = 2 To Rcount
' find the frist occurrence of Col
If .Cells(R, "A").Interior.Color = Col Then Exit For
Next R
If R > Rcount Then
MsgBox "No cells of the specified colour were found.", _
vbInformation, "Unuccessful search"
Else
Rcount = R - 1
Do
.Cells(R, "A").Value = R - Rcount
R = R + 1
' loop until a different cell colour is encountered
Loop While .Cells(R, "A").Interior.Color = Col
End If
End With
End Sub