Get number of row in Range

always_confused

Board Regular
Joined
Feb 19, 2021
Messages
68
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I am trying to write a Sub in which I need the number of rows in a Range which is input as a parameter of the Sub. I can't find the syntax to get the number of rows in the range. For other functions where the range is already known (in this case Range("A:A")) when writing, I've been using:

VBA Code:
lastrow = Sheets("sheetname").Range("A" & Rows.count).End(xlUp).Row

But I'm not sure how to do the same thing in the case where I have

VBA Code:
Dim column As Range
Set column = Range(M:M)

How can I get the number of used rows of Range(column)?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
If you have same row at column M with Column A then

VBA Code:
Dim column As Range, Lastrow as Long
lastrow = Sheets("sheetname").Range("A" & Rows.count).End(xlUp).Row
Set column = Sheets("sheetname").Range("M2:M" & lastrow)
 
Upvote 0
If you have same row at column M with Column A then

VBA Code:
Dim column As Range, Lastrow as Long
lastrow = Sheets("sheetname").Range("A" & Rows.count).End(xlUp).Row
Set column = Sheets("sheetname").Range("M2:M" & lastrow)
I don't have the same number of rows. I would like to get the number of rows in Range(column). Column is whatever I input into the Sub, it's length has nothing to do with other rows
 
Upvote 0
Try this:
VBA Code:
Dim column As Range
Set column = Range(Range("M2"), Range("M" & Rows.Count).End(xlUp))
 
Upvote 0
Try this:
VBA Code:
Dim column As Range
Set column = Range(Range("M2"), Range("M" & Rows.Count).End(xlUp))
I appreciate this but I don't think you're understanding my problem. I have a Sub like this:

VBA Code:
Sub (column As Range)

For i = 1 To last_row_of_column
' do some stuff
Next i

End Sub

How do I get last_row_of_column ?
 
Upvote 0
Just changed column from Range to String = to the column letter instead of the whole column and it's fine now
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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