VBA code to count cells to down until first non blank

Philip00

New Member
Joined
Aug 16, 2017
Messages
37
Hi All,

I need a code that count the cells all the way down until the first non blank cell.

Activecell
Blank
Blank
Blank
NonBlank

So in this way I need 3+1 that is 4

I need this with active cell so I can refer this number in the code later like

X= VBAcode (i got the number here)

and later

If X = 4 then and so on

Can somebody help me with this?

Thank you in Advance,

Philip
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Does this help?

Code:
Function RowsToNextNonBlank() As Long
    Dim l As Long 'counter
    
    'initialise counter
    l = 1
    'Find next cell with a value. Stop after 1000 tries to prevent over long loop
    Do Until ActiveCell.Offset(l).Value <> ""
        l = l + 1
        'if over 1000 assume no more values
        If l > 1000 Then
            RowsToNextNonBlank = 0
            MsgBox "Next none-blank cell not found"
            Exit Function
        End If
    Loop
    
    'return the number stored in 'l'
    RowsToNextNonBlank = l


End Function


Sub Test()
    Dim x As Long
    
    x = RowsToNextNonBlank
    MsgBox x
End Sub
 
Upvote 0
Does this help?

Code:
Function RowsToNextNonBlank() As Long
    Dim l As Long 'counter
    
    'initialise counter
    l = 1
    'Find next cell with a value. Stop after 1000 tries to prevent over long loop
    Do Until ActiveCell.Offset(l).Value <> ""
        l = l + 1
        'if over 1000 assume no more values
        If l > 1000 Then
            RowsToNextNonBlank = 0
            MsgBox "Next none-blank cell not found"
            Exit Function
        End If
    Loop
    
    'return the number stored in 'l'
    RowsToNextNonBlank = l


End Function


Sub Test()
    Dim x As Long
    
    x = RowsToNextNonBlank
    MsgBox x
End Sub

Yes! Exactly that is needed for me. Thank you so much!!!
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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