Why doesn't this worksheet level macro work?

kwp004

Board Regular
Joined
Dec 27, 2016
Messages
93
Why doesn't the VBA below work? If range Z1 = "NO", nothing happens. Thanks!


Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Range("Z1") = "NO" Then


 'strikethrough
    
    Range("A1:Q14").Select
    With Selection.Font
    .Strikethrough = True
    End With
    
    Range("G15:K21").Select
    With Selection.Font
    .Strikethrough = True
    End With
    
    Range("A24:Q30").Select
    With Selection.Font
    .Strikethrough = True
    End With
   
    End If
    



If Range("Z1") = "YES" Then
   
     Range("A1:Q14").Select
    With Selection.Font
    .Strikethrough = False
    End With
    
    Range("G15:K21").Select
    With Selection.Font
    .Strikethrough = False
    End With
    
    Range("A24:Q30").Select
    With Selection.Font
    .Strikethrough = False
    End With
    
    
    End If
    
    End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Does it do anything if Z1 = "YES" ? Also, this probably will not fix the "nothing happens" issue, but you can remove the Select and With Selection portions. Like so:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Range("Z1") = "NO" Then


 'strikethrough
    
    
Range("A1:Q14").Font.Strikethrough = True
Range("G15:K21").Font.Strikethrough = True
Range("A24:Q30").Font.Strikethrough = True

   
 End If
    



If Range("Z1") = "YES" Then
   
Range("A1:Q14").Font.Strikethrough = False
Range("G15:K21").Font.Strikethrough = False
Range("A24:Q30").Font.Strikethrough = False    
    
End If
    
End Sub

Edit: This simplified code seems to work as intended, so maybe it did fix it? I will try with your original code and let you know.
 
Last edited:
Upvote 0
Both versions of code are working for me with the same results.
 
Upvote 0
You know string comparisons in VBA are case-sensitive by default?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Select Case LCase(Range("Z1").Value2)
    Case "no"
      Range("A1:Q14, G15:K21, A24:Q30").Font.Strikethrough = True
    Case "yes"
      Range("A1:Q14, G15:K21, A24:Q30").Font.Strikethrough = False
  End Select
End Sub

And if Z1 is being manually changed, you can preserve Undo for all other cases:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$Z$1" Then
    Select Case LCase(Range("Z1").Value2)
      Case "no"
        Range("A1:Q14, G15:K21, A24:Q30").Font.Strikethrough = True
      Case "yes"
        Range("A1:Q14, G15:K21, A24:Q30").Font.Strikethrough = False
    End Select
  End If
End Sub
 
Upvote 0
Or, if you'd like a RR one-liner,

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$Z$1" Then Range("A1:Q14, G15:K21, A24:Q30").Font.Strikethrough = Switch(LCase(Range("Z1").Value2) = "no", True, LCase(Range("Z1").Value2) = "yes", False)
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top