How to determine the last row in a given worksheet

  • Thread starter Thread starter C C
  • Start date Start date
C

C C

Hello.

I have a macro that formats the worksheet from a text file to an excel file.
I insert a column and create a formula on the second row of that column.
How do I determine the last row in the worksheet so I will paste my
formula until the last row on the new column?

Thanks in advance.
 
Thanks. So after getting the "lastrow"
how do you, say, select the range from A2 to
the lastrow on column A?

Thanks again.
 
Dim LastRow as long

with activesheet
lastrow = .cells(.rows.count,"A").end(xlup)
.range("a2:a" & lastrow).select
end with

Is one way.

Although there is very little that requires that you select a range before you
work with it.
 
Hi C C,

Try:
'===========>>
Sub aTester()
Dim LRow As Long
Const col As String = "B" '<<==== CHANGE

LRow = Cells(Rows.Count, "A").End(xlUp).Row
With Range(col & 2)
.AutoFill Destination:=.Resize(LRow - 1)
End With

End Sub
'<<===========

Change B to reflect the new column.
 
Small omission by Dave

Dave Peterson said:
Dim LastRow as long

with activesheet
lastrow = .cells(.rows.count,"A").end(xlup)

should be

lastrow = .cells(.rows.count,"A").end(xlup).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

Back
Top