问题描述
我有一个窗体控制按钮,我想使用它来对列进行分组/取消分组.也就是说,如果它是第一次分组/隐藏这些列时单击它,而下次单击它则将这些列取消隐藏.
I have a form control button which i want to use to group-ungroup columns.That is if it is clicked the first time it groups/hides those columns and next time it it clicked it unhides those columns.
我想计算该按钮的点击次数,以便如果包含 clicks
计数的 variable
为 odd
如果它是 even
,则将隐藏该列,而我将取消隐藏该列.
I want to count the no of click on that button so that ,if the variable
containing the no of clicks
count is odd
i will hide the columns else if it is even
i will unhide the column.
这是我的代码
Private Sub CommandButton1_Click()
Static cnt As Long
cnt = 0
Dim remain As Integer
cnt = cnt + 1
remain = cnt Mod 2
If remain = 1 Then
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
End If
If remain = 2 Then
ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=2
End If
End Sub
所以我该如何计算vba中变量中该按钮的点击次数.对不起英语不好?
So how can i count the no of clicks on that button in a variable in vba.Sorry for the bad english?
推荐答案
好吧,您不需要使用计数并继续添加.您可以改用 Boolean
变量.这是一个例子.这是一个 ON/OFF
开关.
Ok you do not need to use a count and keep adding to it. You can use a Boolean
Variable instead. Here is an example. This works an an ON/OFF
switch.
Option Explicit
Dim boolOn As Boolean
Sub CommandButton1_Click()
If boolOn = False Then
boolOn = True
MsgBox "OFF"
'
'~~> Do what you want to do
'
Else
boolOn = False
'
'~~> Do what you want to do
'
MsgBox "ON"
End If
End Sub
这篇关于计算excel vba中按钮的点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!