Find last non-blank cell in range

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello community,

I am trying to find last non-blank cell in F2:O2
I tried this way. But it finds first
VBA Code:
    Dim c
    For Each c In sh.Range(Cells(2, 6 + x).Address, Cells(2, 6 + x).Offset(0, 9))
        If c = "" Then
         lc = c.Column
            Exit For
        End If
    Next
 If lc = 0 Then lc = 9 + x

For example in following example I need to set range from Younger to Older. But my code stops at middle age

YOUNGERMIDDLE AGEOLDER
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Unless you really need VBA for that, if the cells contain text, then this will return the column number of the last non blank cell:

Excel Formula:
=COLUMN(F2)+MATCH("",F2:O2,-1)-1
 
Upvote 0
Not sure what you want to do with it once you find it, but...
VBA Code:
Sub Find_Last_Cell()
    Dim c As Range
    Set c = ActiveSheet.Range("F2:O2").SpecialCells(xlCellTypeLastCell)
    MsgBox c.Address
End Sub
 
Upvote 0
Your code gave me some strange result $AOZ$2057
That would have been the last cell where there had been some data at some point in time. Is there anything in that cell now? A formula or a space? If not, save your file & run it again.
 
Upvote 0
I guess I miss explained my situation.

as you can see in my code, it is loop, it needs to find last non-blank cell within F2:O2 then next loop will be P2:Y2 and so on..
 
Upvote 0
Here's an alternative:
VBA Code:
Dim c As Integer, lastRow As Integer
lastRow = 0
For c = Range("F1").Column To Range("O1").Column
    If lastRow < sh.Cells(Rows.Count, c).End(xlUp).Row Then lastRow = sh.Cells(Rows.Count, c).End(xlUp).Row
Next c
MsgBox lastRow
 
Upvote 0
Thanks for reply, but I don't need last row. I need last non-blank within F2:O2
So result should be something like J2
 
Upvote 0
How about this?
VBA Code:
Dim c As Integer, lastRow As Integer, column As Integer
lastRow = 0
x = 0
column = 1
For c = Cells(1, 6 + x).column To Cells(1, 6 + x + 9).column
    If lastRow < sh.Cells(Rows.Count, c).End(xlUp).Row Then
        lastRow = sh.Cells(Rows.Count, c).End(xlUp).Row
        column = c
    End If
Next c
MsgBox Split(Cells(1, column).Address, "$")(1) & lastRow
 
Upvote 0
Understood now :)

Try this
VBA Code:
Sub Find_Last_Cell_2()
    Dim rng As Range, i As Long, LCol As Long
    i = 6
    Do While i < (10 * 3) '<<< change 2nd number to match the number of loops you want to do
        Set rng = Cells(2, i).Resize(, 10)
        On Error GoTo skip
        LCol = rng.Find("*", , xlFormulas, , 2, 2).Column
        MsgBox "Last cell in this range is " & Cells(2, LCol).Address
        i = i + 10
    Loop
skip:
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,404
Messages
6,124,715
Members
449,184
Latest member
COrmerod

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