Toggle cell value inclusion in count formula

MacroShark

New Member
Joined
Jun 5, 2018
Messages
6
In Excel 2010 I have a range of cells that each contains a formula. This range of cells is counted using '=COUNT("Range")'.

I would like to use VBA to exclude a cell from being counted, but also being able to change my mind and include it again. So I want to be able to toggle if a cell is included or not. Using standard Excel (delete and undo) wont help me since the toggle could happen over time.

Any suggestions on how to achieve this? I would greatly appreciate it.

The shark
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
you could do this without VBA

with data validation in cell A1, source list:= true,false
then user this formula in a cell, where the 2nd COUNT excludes the cell (below A5 is not included in the count)
=IF(A1=TRUE,COUNT(A2:A9),COUNT(A2:A4,A6:A9))
Toggling that cell between true and false (via the dropdown) does what you want
 
Last edited:
Upvote 0
Thank you Yongle for your suggestion.

I am looking for a more flexible solution though. It could be that there is one or more cells in the range I want exclude/include. So really what I am looking for is some way to get the count formula to ignore a cell value.
 
Upvote 0
Try something like this.
The input box allows user to select one or more cells for exclusion
Using shift key allows for contiguous cells
Holding down Ctrl key alows individual cells to be selected
Amend the range to reflect your required range

Code:
Sub CountAsRequired()
    Dim rng1 As Range, rng2 As Range, c1 As Long, c2 As Long, msg As String
    msg = "Select cells to be excluded from count" & vbCr & "Use shift\ ctrl keys to select multiple cells"
    Set rng1 = Range("[COLOR=#ff0000]A1:J8[/COLOR]")
    c2 = 0
    c1 = WorksheetFunction.Count(rng1)
    If MsgBox("Count all cells?", vbYesNo, "Count") = vbNo Then
        Set rng2 = Application.InputBox(msg, "EXCLUDE", , , , , , 8)
        c2 = WorksheetFunction.Count(rng2)
    End If
    MsgBox "Count = " & c1 - c2
End Sub
 
Upvote 0
Hehe
You could also use data validation on the column and only allow say y and n.
That way someone cannot enter not, yes.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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