1

I have a table of calculations which all cells have formulas. I want a checkbox which when ticked replaces all cells in the table which are 0 with the word "None".

I have seen some suggestions using formulas.

I tried a few ways but can't figure out how to write a checkbox macro in which two variables need to be met to run the macro.

2
  • 1
    Please share what you have! Commented Jun 12, 2023 at 14:49
  • I doubt you need a macro and the checkbox implies that sometimes your want on "None" and sometimes you want "0" - is that true? Also be aware that having mixed types in a column means you won't be able to use it for any subsequent calculations. An alternative is to use Accounting format which will display "-" for 0 Commented Jun 12, 2023 at 15:42

3 Answers 3

4

You shouldn't modify your data unless it is really necessary. In this case, you can achieve your goal by setting the NumberFormat of the cells instead.

The number format in Excel contains up to 4 part (that are separated by ";"). The first part is for positive numbers, the second is for negative numbers, the third is for 0 (and the forth for strings, but we can igore that). A possible number format would be General;General;"None": For everything except 0, the General format would apply. For 0, Excel would display None (but note that the value of the cell doesn't change, only the way it is displayed in the sheet).

Now all you have to do is:
o Go to the VBA Editor, add a Module (if you don't have any) and add the following code:

Sub ShowHideZeros()
    With ThisWorkbook.Sheets("Sheet1")  ' Replace with your sheet name
        Dim showNone As Boolean
        showNone = (.CheckBoxes(1).Value = xlOn)
        Dim r as Range
        ' Use this to format all cells
        Set r = .UsedRange
        ' Use this instead if you want to format only specific cells:
        ' Set r = .Range("A2:A100")
        If showNone Then
            .UsedRange.NumberFormat = "General;General;""None"""
        Else
            .UsedRange.NumberFormat = "General"
        End If
    End With
End Sub

o Place a checkbox on your sheet and give it a meaningfull text, eg Show "None" for 0. Note that there are 2 different types of checkboxes, this example assumes that you use a so called "Form-Control" (that's the upper part of the control selection).

enter image description here

o Right click on the checkbox and choose "Assign Macro". Select the routine ShowHideZeros.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the valuable lesson. I wasn't aware of most of it.
2

"I have a table of calculations which all already have formulas in them". I think this is the key. The formulas are there and have to stay there. So we can set the formating:

Private Sub CheckBox1_Click()   'ActiveX checkBox
   Call toggleNone
   With CheckBox1
      If .value Then
         .Caption = "Zeros as 0"
      Else
         .Caption = "Zeros as None"
      End If
   End With
End Sub

Sub toggleNone()
   Dim t As ListObject, r As Range, setNone As Boolean
   setNone = Me.CheckBox1.value
   Set t = Me.ListObjects("CALCTABLE")
   For Each r In t.DataBodyRange
      If r.Value2 = 0 Then
         If setNone Then
   'IN NEXT LINE INSTEAD OF ; USE THE LOCAL LIST SEPARATOR (maybe , )
            r.NumberFormat = " ; ; ""None"" ; -"
         Else
            r.NumberFormat = "General"
         End If
      Else
         r.NumberFormat = "General"
      End If
   Next
End Sub

enter image description here

Comments

1

Toggle Matching Values Using a Forms Check Box

enter image description here

  • It is assumed that your data is a table starting in A1 with one row of headers.
Option Explicit

Sub ToggleNone()
    
    Const WorksheetName As String = "Sheet1"
    Const CheckBoxName As String = "Check Box 1"
    Const CellString As String = "None"
    Const CellNumber As Double = 0
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Sheets(WorksheetName)
    
    Dim rg As Range
    
    With ws.Range("A1").CurrentRegion
        Set rg = .Resize(.Rows.Count - 1).Offset(1)
    End With
    
    Dim chkValue As Long: chkValue = ws.CheckBoxes(CheckBoxName).Value
    
    Dim OldValue, NewValue, VarValue As Long
    
    If chkValue = 1 Then ' Checked (1)
        OldValue = CellNumber
        NewValue = CellString
        VarValue = vbDouble
    Else ' Unchecked (-4146)
        OldValue = CellString
        NewValue = CellNumber
        VarValue = vbString
    End If
        
    Dim urg As Range, cell As Range, CurrentValue
    
    ' Combine
    For Each cell In rg.Cells
        CurrentValue = cell.Value
        If VarType(CurrentValue) = VarValue Then
            If CurrentValue = OldValue Then
                If urg Is Nothing Then
                    Set urg = cell
                Else
                    Set urg = Union(urg, cell)
                End If
            End If
        End If
    Next cell
    
    ' Replace
    If Not urg Is Nothing Then
        Dim arg As Range
        For Each arg In urg.Areas
            arg.Value = NewValue
        Next arg
    End If

End Sub

2 Comments

Note that 1 = xlOn and -4146 = xlOff
Thanks. Apparently, I knew it 3 years ago. Getting old.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.