VBA find first non 0 value in range then count how many rows until next non 0 value

VBAhelpplz

New Member
Joined
Mar 24, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi I am at a loss how to do this. I have a range of numbers in the column. Some are 0s and some are not. I need to:

1) find first non 0 value in the range (SaaS column, would correspond to 1/1/24 and $25000
2) count how many rows until next non 0 value in the range (10/1/2024 and $30000)
3) if count is >12 display error
4) do until entire range is checked and there are no instances where the rows with 0s between rows with non-zeros is >12 left. Any help would be greatly appreciated. Here is the data:

1679700180437.png
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
For example:
VBA Code:
Dim ZnZ, aRan
Dim I As Long, Min0 As Long, nwMin As Long

aRan = Range(Range("F1"), Range("F1").Offset(10000, 0).End(xlUp)).Address   '<<< YOUR Range

ZnZ = Evaluate("(IF(" & aRan & "<>0,ROW(" & aRan & "),99999))")
Min0 = Application.WorksheetFunction.Small(ZnZ, 1)
For I = 2 To UBound(ZnZ)
    nwMin = Application.WorksheetFunction.Small(ZnZ, I)
    Debug.Print nwMin, Min0                                 'Debug only
    If nwMin = 99999 Then Exit For
    If nwMin - Min0 > 12 Then
        '
        '   What to do if delta > 12 ??
        '   nwMin is the row#
        '
        MsgBox (nwMin & " after " & Min0)
        '
    Else
        'anything to do?
    End If
    Min0 = nwMin
Next I
You have to customize the line marked <<< (your range to be examined) and specify what to do when a gap >12 is found, removing the MsgBox I used to report the status
 
Upvote 0
Try
Sub MoreThanTwelve()
Dim M, T&
'Saas range is B2:B30
M = Filter(Evaluate("(transpose(if(B2:B30>0,Row(B2:B30),false))"), False, False)
For T = 1 To UBound(M)
If M(T) - M(T - 1) > 12 Then Range("C" & M(T)) = "Error"
Next T
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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