计算excel vba中按钮的点击次数 [英] count no of clicks on a button in excel vba

查看:438
本文介绍了计算excel vba中按钮的点击次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗体控制按钮,我想使用它来对列进行分组/取消分组.也就是说,如果它是第一次分组/隐藏这些列时单击它,而下次单击它则将这些列取消隐藏.

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中按钮的点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆