我有一个支持宏的工作簿,在过去它一直运行良好,但自从我有一个Windows更新后,宏导致工作簿崩溃,给我一个错误“运行时错误'424' : 所需对象。”我似乎无法弄清楚为什么我的代码不再有效。这是我的代码。
Private Sub Worksheet_Change(ByVal Target As Range)
'Rage Button
If Cells(1, 12).Value = "Barbarian" Then 'CHANGE THE CELL TO THE ADRESS OF THE TRIGGER CELL
Rage.Visible = True 'CHANGE TO THE NAME OF THE BUTTON
Else
Rage.Visible = False 'CHANGE TO THE NAME OF THE BUTTON
End If
'Raging Brutality
If Cells(1, 12).Value = "Barbarian" Then
If WorksheetFunction.CountIf(Range(Cells(40, 1), Cells(61, 1)), "Raging Brutality") Then
Brutality.Visible = True
Else
Brutality.Visible = False
End If
Else
Brutality.Visible = False
End If
'Sneak Button
If Cells(1, 12).Value = "Rogue" Then 'CHANGE THE CELL TO THE ADRESS OF THE TRIGGER CELL
Sneak.Visible = True 'CHANGE TO THE NAME OF THE BUTTON
Else
Sneak.Visible = False 'CHANGE TO THE NAME OF THE BUTTON
Cells(25, 7).Value = 0
End If
End Sub
答案 0 :(得分:2)
不是您特定问题的答案,但由于Visible
是布尔属性,因此可以减少代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Rage.Visible = (Cells(1, 12).Value = "Barbarian")
Brutality.Visible = (Cells(1, 12).Value = "Barbarian" And _
WorksheetFunction.CountIf( _
Cells(40, 1).Resize(22,1), "Raging Brutality") > 0)
Sneak.Visible = (Cells(1, 12).Value = "Rogue")
If Not Sneak.Visible Then Cells(25, 7).Value = 0
End Sub
答案 1 :(得分:0)
您要么首先要定义对象
Rage = ActiveSheet.Shapes("Rage")
或以他们接受的格式引用它们,例如
ActiveSheet.Shapes("Rage").Visible = False
。
使用Tim Williams' snipplet,您的代码将如下所示:
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Shapes("Rage").Visible = (Cells(1, 12).Value = "Barbarian")
ActiveSheet.Shapes("Brutality").Visible = (Cells(1, 12).Value = "Barbarian" And _
WorksheetFunction.CountIf( _
Cells(40, 1).Resize(22,1), "Raging Brutality") > 0)
ActiveSheet.Shapes("Sneak").Visible = (Cells(1, 12).Value = "Rogue")
If Not ActiveSheet.Shapes("Sneak").Visible Then Cells(25, 7).Value = 0
End Sub
选择name box后,确保您的对象名为 Rage , Brutality 和 Sneak 。
更新: 我做了一个更清晰简洁的分 看起来你不能重命名按钮,只需查看他们的名字,并替换为 Ctrl + H ,为每个名称(Rage,Brutality,Sneak)。
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Shapes("Rage").Visible = False 'let's make all invisible before evaluation
ActiveSheet.Shapes("Brutality").Visible = False
ActiveSheet.Shapes("Sneak").Visible = False
Select Case Cells(1, 12).Value
Case "Barbarian"
ActiveSheet.Shapes("Rage").Visible = True
If WorksheetFunction.CountIf(Range(Cells(40, 1), Cells(61, 1)), "Raging Brutality") Then
ActiveSheet.Shapes("Brutality").Visible = True
End If
Case "Rogue"
ActiveSheet.Shapes("Sneak").Visible = True
Case Else
Cells(25, 7).Value = 0
End Select
End Sub