VBA Range Question

D

Don

In VBA how do I find the first and last row number in a range?

I need to perform various calculations on different segments of a range
(single column range) and my first problem is to identify the first and last
row. Then I'll loop through the rows and perform various calculations.

Thanks.

Don

P.S. Is there a VBA reference somewhere that would help me answer questions
like above? Using the VBA help facility doesn't seem to help, probably
because of my low expertise in VBA.
 
P

Per Jessen

Look at this:

Sub aaa()
Dim MyRange As Range
Set MyRange = Range("A10:A20")
FirstRow = MyRange.Row
If FirstRow <> 1 Then
LastRow = MyRange.Rows.Count + FirstRow - 1
Else
LastRow = MyRange.Rows.Count
End If
MsgBox ("FirstRow: " & FirstRow & vbLf & "LastRow: " & LastRow)

End Sub

Hopes this helps.
....
Per
 
R

Rick Rothstein

Try this...

Dim R As Range, FirstRow As Long, LastRow As Long
.....
.....
' Set your range to a Range variable, no
' matter how the range gets specified.
Set R = Range("A3:A345")
' Then use cell iterating to move within the range
FirstRow = R(1).Row
LastRow = R(R.Count).Row
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top