Use Lastrow found as a Public Constant?

ArnMan

Board Regular
Joined
Aug 30, 2017
Messages
69
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello,

I am wondering how I can add my lRow as a public const so I don't have the same code in every module doing the same thing

This is what I am using to get my last row used and works great.

Code:
    Dim lRow As Long
    lRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row + 1

I am hoping to add this as a Public const so all I have to do is just add lRow to whatever cell or range I am working on at the time.
I've tried this
Code:
Public Const lRow As Long = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row + 1
But vba doesn't like that. it highlights the .Row for some reason.

Thank you in advance.
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Re: How can I use Lastrow found as a Public Constant?

Code:
Public Const lRow As Long = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row + 1
But vba doesn't like that. it highlights the .Row for some reason.
You cannot include any function or property calls when defining a constant as they need to be evaluated before a value is returned from them. The way constants work, as I understand it, is the constant value physically replaces the constant names in the code as a first step before the compiler begins doing its work, so the function or property does not have a value at that point in time.
 
Last edited:
Upvote 0
Re: How can I use Lastrow found as a Public Constant?

ArnMan,

See if the following will work for you:

Code:
Option Explicit

Public lRow As Long

Sub Test()

lRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row + 1

End Sub
 
Upvote 0
Re: How can I use Lastrow found as a Public Constant?

Thank you for the quick response. I put that in a seperate module, and used Call Test where it is needed. That way I only need to use that instead of having to retype or copy and Paste my code in all the other modules I have going

ArnMan,

See if the following will work for you:

Code:
Option Explicit

Public lRow As Long

Sub Test()

lRow = Sheet1.Cells(Rows.Count, "A").End(xlUp).Row + 1

End Sub
 
Upvote 0
Re: How can I use Lastrow found as a Public Constant?

Thank you that actually explained it easy enough for me to understand what was going

Thank you again
You cannot include any function or property calls when defining a constant as they need to be evaluated before a value is returned from them. The way constants work, as I understand it, is the constant value physically replaces the constant names in the code as a first step before the compiler begins doing its work, so the function or property does not have a value at that point in time.
 
Upvote 0
Re: How can I use Lastrow found as a Public Constant?

I tend to use the following public function to return the Last Row of a specified sheet and column, which I can then call from any module, removing need for a public constant:
Code:
Public Function LastRow(ByRef wks As Worksheet, Optional ByRef col As Long = 1) As Long
    
    With wks
        LastRow = .Cells(.Rows.count, col).End(xlUp).row
    End With

End Function
Then in any code, I can use:
Code:
Sub Dummy()

Dim LR as Long
Dim wks as Worksheet

Set wks = Sheets("Sheet1")

LR = LastRow(wks, 2)

End Sub
Here function LastRow returns the row number of the last used cell in column 2 (B) to variable LR within Sub Dummy. If I have another sub, I can call the same function again.

If I use
Code:
LR = LastRow(wks)
it defaults the column to check as 1 (A)
 
Last edited:
Upvote 0
Re: How can I use Lastrow found as a Public Constant?

Thank you for the quick response. I put that in a seperate module, and used Call Test where it is needed. That way I only need to use that instead of having to retype or copy and Paste my code in all the other modules I have going

ArnMan,

Thanks for the feedback.

You are very welcome.

Nicely done.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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