Worksheet_Calculate隐藏命令按钮

时间:2017-12-08 05:00:28

标签: excel excel-vba vba

我在Worksheet_Change找到了以下代码。但我的H29是公式=SUM。 如何将其更改为Worksheet_Calculate以便以下宏运行?

基本上我想要的是如果H29被计算为等于20,则显示按钮,否则隐藏它。

Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False

If Target = Range("H29") Then
If Target.Value = "20" Then
Me.CommandButton1.Visible = True


Else

Me.CommandButton1.Visible = False

End If
Application.ScreenUpdating = True
End If

2 个答案:

答案 0 :(得分:1)

根据按钮类型,指示按钮的代码将有所不同。试试这个。

Private Sub Worksheet_Calculate()
'ActiveX button
If Range("H29").Value = 20 Then
  Sheets("Sheet1").CommandButton2.Visible = False
Else
  Sheets("Sheet1").CommandButton2.Visible = True
End If

'Forms Button
If Range("H29").Value = 20 Then
  Sheets("Sheet1").Shapes("CommandButton1").Visible = msoFalse
Else
  Sheets("Sheet1").Shapes("CommandButton1").Visible = msoTrue
End If

End Sub

答案 1 :(得分:0)

抱歉太快了。解决

Private Sub Worksheet_Calculate()
'ActiveX button
Application.ScreenUpdating = False
If Range("H29").Value = 20 Then
  Me.CommandButton1.Visible = True
Else
   Me.CommandButton1.Visible = False
End If
Application.ScreenUpdating = True
End Sub