单击框时突出显示和取消突出显示特定范围

时间:2022-11-21 20:48:29

I am a beginning in visual basic. What I am trying to do is whenever a box is clicked, highlight a specific range. Then, if another box is clicked after that, the previous range will unhighlight, and another range will highlight. Here is my code but it doesn't work right now.

我是视觉基础的开始。我想要做的是每当点击一个框时,突出显示一个特定的范围。然后,如果在此之后单击另一个框,则前一个范围将不会突出显示,另一个范围将突出显示。这是我的代码但它现在不起作用。

Dim FSelect As Boolean
Dim myRange As Range

Sub Rectangle18_Click()

   If FSelect Then
        UnhighlightBox (myRange) <---error - runtime error "424" object required
   End If

   Range("C9:D9").Select
   HighlightBox

   FSelect = True

   Set myRange = Range("C9:D9")
   End Sub

Sub Rectangle19_Click()

   If FSelect Then
        UnhighlightBox (myRange)
   End If

   Range("C11:D11").Select
   HighlightBox

   FSelect = True

   Set myRange = Range("C11:D11")

End Sub    

Sub HighlightBox()
   Selection.Interior.ColorIndex = 36
End Sub

Sub UnhighlightBox(cellRng As Range)
   cellRng.Interior.ColorIndex = 2
End Sub

3 个解决方案

#1


When I throw this code into excel it complains about Select. I don't think you can use Select as a variable...

当我把这个代码扔进excel时,它会抱怨Select。我不认为你可以使用Select作为变量......

EDIT: Select is a reserved keyword in VB/A, It begins the Select Case block.

编辑:选择是VB / A中的保留关键字,它开始选择案例块。

#2


Putting the parentheses around the argument when calling the UnhighlightBox procedure is incorrect.

在调用UnhighlightBox过程时将括号括在参数周围是不正确的。

Two possible correct forms:

两种可能的正确形式:

UnhighlightBox myRange

Call UnhighlightBox(myRange)

I find the first form (without the Call keyword) to be preferable

我发现第一种形式(没有Call关键字)更可取

For the Excel 2003 help:

对于Excel 2003帮助:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist.

调用过程时,不需要使用Call关键字。但是,如果使用Call关键字调用需要参数的过程,则必须将参数列表括在括号中。如果省略Call关键字,则还必须省略argumentlist周围的括号。

Please note that this does not apply to a function which returns a value. A function needs to be called as part of an assignment (e.g. a = f(x)) and the arguments must be enclosed in parentheses

请注意,这不适用于返回值的函数。函数需要作为赋值的一部分调用(例如a = f(x)),并且参数必须括在括号中

Your use of the FSelect boolean (which initialises to false) should be preventing the problem with calling UnhighlightBox before myRange is ever set.

您使用FSelect布尔值(初始化为false)应该可以防止在设置myRange之前调用UnhighlightBox的问题。

#3


When using the UnhighlightBox routine you either need to put the Call statement before hand or remove the parenthesis.

使用UnhighlightBox例程时,您需要先放置Call​​语句或删除括号。

For example:

Call UnhighlightBox (myRange)

or

UnhighlightBox myRange

For more info on why to use Call, etc see the either of the following:

有关使用Call等的原因的详细信息,请参阅以下任一方法:

what-does-the-call-keyword-do-in-vb6

MSDN

#1


When I throw this code into excel it complains about Select. I don't think you can use Select as a variable...

当我把这个代码扔进excel时,它会抱怨Select。我不认为你可以使用Select作为变量......

EDIT: Select is a reserved keyword in VB/A, It begins the Select Case block.

编辑:选择是VB / A中的保留关键字,它开始选择案例块。

#2


Putting the parentheses around the argument when calling the UnhighlightBox procedure is incorrect.

在调用UnhighlightBox过程时将括号括在参数周围是不正确的。

Two possible correct forms:

两种可能的正确形式:

UnhighlightBox myRange

Call UnhighlightBox(myRange)

I find the first form (without the Call keyword) to be preferable

我发现第一种形式(没有Call关键字)更可取

For the Excel 2003 help:

对于Excel 2003帮助:

You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist.

调用过程时,不需要使用Call关键字。但是,如果使用Call关键字调用需要参数的过程,则必须将参数列表括在括号中。如果省略Call关键字,则还必须省略argumentlist周围的括号。

Please note that this does not apply to a function which returns a value. A function needs to be called as part of an assignment (e.g. a = f(x)) and the arguments must be enclosed in parentheses

请注意,这不适用于返回值的函数。函数需要作为赋值的一部分调用(例如a = f(x)),并且参数必须括在括号中

Your use of the FSelect boolean (which initialises to false) should be preventing the problem with calling UnhighlightBox before myRange is ever set.

您使用FSelect布尔值(初始化为false)应该可以防止在设置myRange之前调用UnhighlightBox的问题。

#3


When using the UnhighlightBox routine you either need to put the Call statement before hand or remove the parenthesis.

使用UnhighlightBox例程时,您需要先放置Call​​语句或删除括号。

For example:

Call UnhighlightBox (myRange)

or

UnhighlightBox myRange

For more info on why to use Call, etc see the either of the following:

有关使用Call等的原因的详细信息,请参阅以下任一方法:

what-does-the-call-keyword-do-in-vb6

MSDN