VBA For Selecting Cells WIth Zero

excelakos

Board Regular
Joined
Jan 22, 2014
Messages
79
Hi everyone!
So i have found below code to help me select all blank cells in a range.
Is there a way to make it select all cells having zero (0) value?
Thank you in advance!!


VBA Code:
Sub SelectBlankCells()


    Range("tablename[columnname]").Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
      
End Sub
 
Hi
I guess
this what you asking for
VBA Code:
Sub test()
    Dim zrng As Range, c As Range
    Dim lr
       lr = Cells(Rows.Count, Selection.Column).End(xlUp).Row
        For Each c In Selection(lr).Offset(-lr + 1).Resize(lr).Cells
            If c.Value = 0 And Not IsEmpty(c) Then
                If zrng Is Nothing Then
                    Set zrng = c
                Else
                    Set zrng = Union(zrng, c)
                End If
            End If
        Next
        If Not zrng Is Nothing Then zrng.Select
End Sub
 
Upvote 0

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi
I guess
this what you asking for
VBA Code:
Sub test()
    Dim zrng As Range, c As Range
    Dim lr
       lr = Cells(Rows.Count, Selection.Column).End(xlUp).Row
        For Each c In Selection(lr).Offset(-lr + 1).Resize(lr).Cells
            If c.Value = 0 And Not IsEmpty(c) Then
                If zrng Is Nothing Then
                    Set zrng = c
                Else
                    Set zrng = Union(zrng, c)
                End If
            End If
        Next
        If Not zrng Is Nothing Then zrng.Select
End Sub
Oh Lord!!! Mohadin you are 90% there!!! Yes the action is restrained in the active column 100% as desired, BUT it goes and select all cells with zeros below the already top selected cell. See attached screen shot
 

Attachments

  • Mrexcel3.png
    Mrexcel3.png
    38.6 KB · Views: 6
Upvote 0
VBA Code:
Sub testr()
    Dim zrng As Range, c As Range
    Dim lr
       lr = Cells(Rows.Count, Selection.Column).End(xlUp).Row
        For Each c In Selection.Offset(-lr + (Selection.Row + 2)).Resize(lr).Resize(lr).Cells
            If c.Value = 0 And Not IsEmpty(c) Then
                If zrng Is Nothing Then
                    Set zrng = c
                Else
                    Set zrng = Union(zrng, c)
                End If
            End If
        Next
        If Not zrng Is Nothing Then zrng.Select
End Sub
 
Upvote 0
VBA Code:
Sub testr()
    Dim zrng As Range, c As Range
    Dim lr
       lr = Cells(Rows.Count, Selection.Column).End(xlUp).Row
        For Each c In Selection.Offset(-lr + (Selection.Row + 2)).Resize(lr).Resize(lr).Cells
            If c.Value = 0 And Not IsEmpty(c) Then
                If zrng Is Nothing Then
                    Set zrng = c
                Else
                    Set zrng = Union(zrng, c)
                End If
            End If
        Next
        If Not zrng Is Nothing Then zrng.Select
End Sub
Now we get 1004 error
 

Attachments

  • Mrexcel4.png
    Mrexcel4.png
    21.7 KB · Views: 3
Upvote 0
VBA Code:
For Each c In Selection.Offset(-lr + (Selection.Row + 2)).Resize(lr).Cells
Sorry!
 
Upvote 0
Maybe this would suit your purpose?

VBA Code:
Sub ZeroesV2()
    Dim SearchRNG As Range, ZeroRNG As Range, Cell As Range
    Dim wb As Workbook, wbs As Worksheet
        Set wb = ThisWorkbook
        Set wbs = wb.ActiveSheet
        
        Set SearchRNG = Selection

        Set SearchRNG2 = SearchRNG.ListObject.ListColumns(SearchRNG.Column - SearchRNG.ListObject.DataBodyRange.Column + 1).DataBodyRange
    
        For Each Cell In SearchRNG2
            If Cell.Value = 0 And Not IsEmpty(Cell) And Cell.Row > SearchRNG.Row Then
                If ZeroRNG Is Nothing Then
                    Set ZeroRNG = Cell
                Else
                    Set ZeroRNG = Union(ZeroRNG, Cell)
                End If
            End If
        Next
        
        If Not ZeroRNG Is Nothing Then
            ZeroRNG.Select
        Else
            MsgBox "No zero values were not found in the selected column"
        End If
End Sub
 
Upvote 0
I understand wrong, sorry.
Try this:

VBA Code:
Sub ZeroesV3()
    Dim SearchRNG As Range, ZeroRNG As Range, Cell As Range
    Dim wb As Workbook, wbs As Worksheet
        Set wb = ThisWorkbook
        Set wbs = wb.ActiveSheet
        
        Set SearchRNG = Selection

        Set SearchRNG = SearchRNG.ListObject.ListColumns(SearchRNG.Column - SearchRNG.ListObject.DataBodyRange.Column + 1).DataBodyRange
    
        For Each Cell In SearchRNG
            If Cell.Value = 0 And Not IsEmpty(Cell) Then
                If ZeroRNG Is Nothing Then
                    Set ZeroRNG = Cell
                Else
                    Set ZeroRNG = Union(ZeroRNG, Cell)
                End If
            End If
        Next
        
        If Not ZeroRNG Is Nothing Then
            ZeroRNG.Select
        Else
            MsgBox "No zero values were not found in the selected column"
        End If
End Sub
 
Upvote 0
Solution
I understand wrong, sorry.
Try this:

VBA Code:
Sub ZeroesV3()
    Dim SearchRNG As Range, ZeroRNG As Range, Cell As Range
    Dim wb As Workbook, wbs As Worksheet
        Set wb = ThisWorkbook
        Set wbs = wb.ActiveSheet
       
        Set SearchRNG = Selection

        Set SearchRNG = SearchRNG.ListObject.ListColumns(SearchRNG.Column - SearchRNG.ListObject.DataBodyRange.Column + 1).DataBodyRange
   
        For Each Cell In SearchRNG
            If Cell.Value = 0 And Not IsEmpty(Cell) Then
                If ZeroRNG Is Nothing Then
                    Set ZeroRNG = Cell
                Else
                    Set ZeroRNG = Union(ZeroRNG, Cell)
                End If
            End If
        Next
       
        If Not ZeroRNG Is Nothing Then
            ZeroRNG.Select
        Else
            MsgBox "No zero values were not found in the selected column"
        End If
End Sub
Bullseye!!!! That nailed it!!!! Thank you both so much. @mohadin thank you too, seems like you pushed each other to the solution!!!
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

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