Last Row

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Good afternoon

Could someone provide an explanation why the below endrow variable is not locating the last row of data in it's current position in the script. It stops format before the last row. This maybe an easy one, I am unable to see it.

Code:
Sub FormatRange()
Dim endrow As Long
endrow = Range("H" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Sheets("Sheet1").Activate
 
With Range("I1:M4")
.VerticalAlignment = xlBottom
.WrapText = True
.HorizontalAlignment = xlCenter
End With
 
Cells(1, 1).Font.Size = 42
Cells(1, 1).Font.Bold = True
 
With Range("A2", Range("M" & Rows.Count).End(xlUp))
.Font.Size = 30
.Font.Name = "Calibri"
End With
 
Range(Cells(6, 1), Cells(6, 13)).Font.Size = 28
 
With Range("I7", Range("I" & endrow))
.NumberFormat = "#,##0.00"
.Font.Size = 30
End With
 
Last edited:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Ron De Bruin wrote some code that is fantastic. Best way to get the last row, column, or cell in a range.

Code:
Option Explicit

 Public Function GetLast(choice As Long, rng As Range)
    'Ron de Bruin, 5 May 2008
    ' 1 = GetLast row
    ' 2 = GetLast column
    ' 3 = GetLast cell
        Dim lrw As Long
        Dim lcol As Long
    
        Select Case choice
    
        Case 1:
            On Error Resume Next
            GetLast = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Row
            On Error GoTo 0
    
        Case 2:
            On Error Resume Next
            GetLast = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0
    
        Case 3:
            On Error Resume Next
            lrw = rng.Find(What:="*", _
                           After:=rng.Cells(1), _
                           lookat:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
            On Error GoTo 0
    
            On Error Resume Next
            lcol = rng.Find(What:="*", _
                            After:=rng.Cells(1), _
                            lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByColumns, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False).Column
            On Error GoTo 0
    
            On Error Resume Next
            GetLast = rng.Parent.Cells(lrw, lcol).Address(False, False)
            If Err.Number > 0 Then
                GetLast = rng.Cells(1).Address(False, False)
                Err.Clear
            End If
            On Error GoTo 0
    
        End Select
    End Function

use that instead. it's more reliable
 
Upvote 0
I'm guessing last row in "M" isn't as far dowwn as lastrow in "H"
Code:
With Range("A2", Range("M" & Rows.Count).End(xlUp))

So why not use
Code:
With Range("A2", Range("M" & endrow))
 
Upvote 0
If I relocate the endrow from the top

Code:
endrow = Range("H" & Rows.Count).End(xlUp).Row

to above this line then I seem to have no problem?

Code:
With Range("I7", Range("I" & endrow))
.NumberFormat = "#,##0.00"
.Font.Size = 30
End With
 
Upvote 0
If I relocate the endrow from the top

Code:
endrow = Range("H" & Rows.Count).End(xlUp).Row

to above this line then I seem to have no problem?

Code:
With Range("I7", Range("I" & endrow))
.NumberFormat = "#,##0.00"
.Font.Size = 30
End With

That's because in the original code you have the code endrow code above where you make Sheet1 the active sheet so it is using the row number of whatever sheet is active at the time.

Code:
endrow = Range("H" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Sheets("Sheet1").Activate
 
Upvote 0
Ron De Bruin wrote some code that is fantastic. Best way to get the last row, column, or cell in a range.

....

use that instead. it's more reliable

Find is no more reliable, it is just a different method, where you have more options for setting the parameters :biggrin: for instance with the code you posted it will find cells with formulas that return "" (as will the xlUp method).

One of the differences is with the Find method you have the option to ignore cells returning "" whereas with the xlUp method you don't have the option.

There are other benefits to the Find method such as you can you can use Find on the entire sheet or multiple columns which you can't with xlUp, but still both methods are reliable (it just depends what your data looks like and what you want to do with it).
 
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