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
 
I made some changes on cmowla's code, now result is correct.
VBA Code:
Dim sh As Worksheet
Set sh = ActiveSheet

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(Columns.Count, c).End(xlUp).column Then
        lastRow = sh.Cells(Columns.Count, c).End(xlUp).column
        column = c
    End If
Next c
MsgBox Split(Cells(1, column).Address, "$")(1) & 2

Kevin thanks for reply , will try and feedback your code.
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Kevin your code shows always 10th column. May be it is because cells are not empty ? there are formulas
 
Upvote 0
If you don't mind using Range.Find, then you can do this without looping:
VBA Code:
Dim x As Integer
x = 0
Dim searchRange As Range, lastNonBlank As Range
Set searchRange = sh.Cells(2, x + 6).Resize(Rows.Count - 1, x + 10)
Set lastNonBlank = sh.Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas)
MsgBox Replace(lastNonBlank.Address, "$", "")
 
Upvote 0
Not works.. I don't need row.count. Row number is 2 Fixed.
 
Upvote 0
Not works.. I don't need row.count. Row number is 2 Fixed.
Rows.Count = 1048576, not 2. I have 2 hardcoded in sh.Cells(2, x + 6).

To get it to work, you will have to tweak it like you did my previous.
 
Upvote 0
May be this image will explain my goal better ?
I need to get marked ranges on each loop
 

Attachments

  • forforum.png
    forforum.png
    15.1 KB · Views: 4
Upvote 0
Looking at how you tweaked my last code, it appears you want to find the last non-blank. But after it's found, you want to only return the column of that non-blank at row 2. (Not the actual non-blank cell, as what my previous code does.) This tweak to my previous code gives this result:
VBA Code:
Dim x As Integer
x = 0
Dim searchRange As Range, lastNonBlank As Range
Set searchRange = sh.Cells(2, x + 6).Resize(Rows.Count - 1, x + 10)
Set lastNonBlank = searchRange.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas)
MsgBox Replace(Cells(2, lastNonBlank.column).Address, "$", "")
EDIT:
And I noticed that with my previous code, it was incorrect for finding the last non-blank cell. Here's the fix (for those interested):
VBA Code:
Dim x As Integer
x = 0
Dim searchRange As Range, lastNonBlank As Range
Set searchRange = sh.Cells(2, x + 6).Resize(Rows.Count - 1, x + 10)
Set lastNonBlank = searchRange.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlFormulas)
MsgBox Replace(lastNonBlank.Address, "$", "")

(I made the same mistake in my first edit of this post, so @Akbarov please see the edited version above.)
 
Last edited:
Upvote 0
Yes as I mentioned before your code worked fine for first loop. But not for next loops.
Here what I tried now, and it always return F2. When I changed rows.count to columns now it return last column in whole worksheet.
VBA Code:
Sub test()

Dim sh As Worksheet
Set sh = ActiveSheet

Dim x As Integer
For x = 0 To 30 Step 10
Dim searchRange As Range, lastNonBlank As Range
Set searchRange = sh.Cells(2, x + 6).Resize(2, x + 10)
Set lastNonBlank = sh.Cells.Find(What:="*", SearchOrder:=xlColumns, SearchDirection:=xlPrevious, LookIn:=xlFormulas)

MsgBox Replace(sh.Cells(2, lastNonBlank.column).Address, "$", "")
Next x
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,412
Messages
6,124,761
Members
449,187
Latest member
hermansoa

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