Find for last value contain cell...

somnath_it2006

Well-known Member
Joined
Apr 11, 2009
Messages
574
Hi All,

I want to find non-blank last cell of the sheet. I have a code but if there is any merge cell is there then its not working.

Code:
lngLastRow = ObjWBTarget.Sheets(ObjWSTarget.Name).Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Row

lngLastCol = ObjWBTarget.Sheets(ObjWSTarget.Name).Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

Above code gives the last cells column and row number.
But not working if merge cell is there.

Please help!!!!!

Thanks in advance....
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,

If your definition of last used row includes the mergearea of the last cell you can account for it using the Range.MergeArea() property:

Code:
Sub test()
    Dim LastRow As Long
 
    LastRow = Get_Last_Used_Row(Sheet1.Cells)
 
    Debug.Print LastRow
 
End Sub
 
Function Get_Last_Used_Row(rngToCheck As Range) As Long
 
    Dim rngFound As Range
 
    Set rngFound = rngToCheck.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious)
 
    If rngFound Is Nothing Then
        Get_Last_Used_Row = rngToCheck.Row
 
    Else
        With rngFound.MergeArea
            Get_Last_Used_Row = .Row + .Rows.Count - 1
        End With
    End If
End Function

Same concept to be applied for the last column. Of course, in an ideal world you would want to try to get rid of those merged cells (IMO)....

Of course, if the mergearea of another cell (not the last cell) goes lower than the last cell, then this function would not account for it. If this is the issue then you could look at using the less reliable Range.SpecialCells(xlCellTypeLastCell) method.

Hope that helps...
 
Last edited:
Upvote 0
Thanks for your reply Colin....
I used Range.SpecialCells(xlCellTypeLastCell) method, and its working fine.


Thanks.
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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