Filter values in certain cells

russelldt

Board Regular
Joined
Feb 27, 2021
Messages
160
Office Version
  1. 365
Platform
  1. MacOS
I have a Macro button that i need to add a Macro that will perform the following in Column M, Cells M4 to M40.

The cells all contain numbers that are variable based on a formula. Some of the calculations have a "0" as the answer

When i click the button the first time, numbers containing 0 are filtered out.

When i click the button again, the filter is removed, and all values are shown

Many thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
See if the following gives you what you want. Change the sheet name to suit.
VBA Code:
Option Explicit
Sub HideZeros()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, rng As Range, c As Range
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    Set rng = ws.Range("M4:M40")
    
    For Each c In rng
        If c.Value = 0 Then
            If c.EntireRow.Hidden = True Then
                c.EntireRow.Hidden = False
            Else
                c.EntireRow.Hidden = True
            End If
        End If
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
See if the following gives you what you want. Change the sheet name to suit.
VBA Code:
Option Explicit
Sub HideZeros()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, rng As Range, c As Range
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    Set rng = ws.Range("M4:M40")
   
    For Each c In rng
        If c.Value = 0 Then
            If c.EntireRow.Hidden = True Then
                c.EntireRow.Hidden = False
            Else
                c.EntireRow.Hidden = True
            End If
        End If
    Next c
    Application.ScreenUpdating = True
End Sub
Works perfectly, thank you
 
Upvote 0
See if the following gives you what you want. Change the sheet name to suit.
VBA Code:
Option Explicit
Sub HideZeros()
    Application.ScreenUpdating = False
    Dim ws As Worksheet, rng As Range, c As Range
    Set ws = Worksheets("Sheet1")   '<~~ *** Change to actual sheet name ***
    Set rng = ws.Range("M4:M40")
   
    For Each c In rng
        If c.Value = 0 Then
            If c.EntireRow.Hidden = True Then
                c.EntireRow.Hidden = False
            Else
                c.EntireRow.Hidden = True
            End If
        End If
    Next c
    Application.ScreenUpdating = True
End Sub
[RIGHT]
geometry dash lite[/RIGHT]
Through these topics, I have a deeper understanding of calculations in excel
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,014
Latest member
Chris258

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