How can i skip empty cells with this loop

mmckay1

New Member
Joined
Mar 29, 2012
Messages
31
im trying to work on a simple program that it will highlight all cells with a value of 5 or less and VB is not my strong point. The problem is it highlights a million cells in the column because empty is less than 5. this is the code I'm using. I fumbled through forums for a day to get this far. I have a "Next without For" error. Any help will be greatly appreciated thanks.
Code:
Sub numfinder()
    Dim cell As Range
    Dim NewRange As Range
    Dim MyCount As Long
    MyCount = 1
    For Each cell In Range("F5:F116")
            If cell.Value = Empty Then Next cell
        If cell.Value <= 5 Then
            If MyCount = 1 Then Set NewRange = cell
            Set NewRange = Application.Union(NewRange, cell)
            MyCount = MyCount + 1
        End If
    Next cell
    NewRange.Interior.ColorIndex = 8
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Like this:

Code:
Sub numfinder()
    Dim cell As Range
    Dim NewRange As Range
    Dim MyCount As Long
    MyCount = 1
    For Each cell In Range("F5:F116")
        If cell.Value <> "" Then
            If cell.Value <= 5 Then
                If MyCount = 1 Then Set NewRange = cell
                Set NewRange = Application.Union(NewRange, cell)
                MyCount = MyCount + 1
            End If
        End If
    Next cell
    NewRange.Interior.ColorIndex = 8
End Sub

Dom
 
Upvote 0
mmckay1,


Sample raw data before the macro:


Excel Workbook
F
59
68
7
86
95
104
11
129
138
14
156
165
174
18
Sheet1





After the macro:


Excel Workbook
F
59
68
7
86
95
104
11
129
138
14
156
165
174
18
Sheet1





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


Code:
Option Explicit
Sub numfinderV2()
' hiker95, 03/29/2012
' http://www.mrexcel.com/forum/showthread.php?t=624952
Dim cell As Range
Dim NewRange As Range
Dim MyCount As Long
MyCount = 1
For Each cell In Range("F5:F116")
  If cell = "" Then
    'do nothing
  ElseIf cell.Value <= 5 Then
    If MyCount = 1 Then Set NewRange = cell
    Set NewRange = Application.Union(NewRange, cell)
    MyCount = MyCount + 1
  End If
Next cell
NewRange.Interior.ColorIndex = 8
End Sub


Then run the numfinderV2 macro.
 
Upvote 0
Re: How can i skip empty cells with this loop *FIXED*

thanks so much domski it works perfectly!
 
Upvote 0

Forum statistics

Threads
1,215,540
Messages
6,125,405
Members
449,223
Latest member
Narrian

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