Do Until函数使用if循环"对象错误"

时间:2015-08-03 02:35:10

标签: excel vba excel-vba

Option Explicit
Sub Autoselect()

Dim Refcolor As Long
Dim RefRow As Long
Dim Refcol As Long
Dim IncRow As Long
Dim RCap As Long

Refcolor = RGB(220, 230, 241)
RefRow = Selection.Row
Refcol = 2  'Will get back to ref column later
RCap = 2000
IncRow = RefRow

Do Until IncRow = RefRow + RCap
If Cells(IncRow, "B").Interior.Color = Refcolor Then
Cells(IncRow, "B").Select
Exit Do
Else
If Cells(IncRow, "B").Value = Nothing Then
Cells(IncRow, "B").Select
Exit Do
Else
End If
IncRow = IncRow + 1
Loop

尝试创建一个函数调用autoselect,它将选择包含颜色RGB(220,230,241)的单元格或在Column" B"上不包含任何值的单元格。函数的开始将来自selection.row。

1 个答案:

答案 0 :(得分:0)

您的错误来自以下声明:

If Cells(IncRow, "B").Value = Nothing Then

Nothing用于对象,而不是内在类型(字符串,数字,布尔值)。要测试Value是否为空/空,请使用IsEmpty()函数:

If IsEmpty(Cells(IncRow, "B").Value) Then