Select dynamic range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear all,

I would like to select a range starting from cell A1, but the ending cell
can be any cell on the worksheet. I am not sure how to program this. Can
anyone advise?
Thanks in advance!

Ivan
 
Here is the basic code:

Sub fLastCell()
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox "Last Row Number is " & LastRow, , ""
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastCell = Cells(LastRow, LastColumn)
End Sub

You will need to select a column that has contiguous
data for Cells(Rows.Count, ?) and a row that has
contiguous data for cells(Columns.count, ?) so that the
End( ) function works properly.
 
I forgot to select the range:

Sub fLastCell()
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox "Last Row Number is " & LastRow, , ""
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastCell = Cells(LastRow, LastColumn).Address 'Also add Address
Range("$A$1:" & LastCell).Select
End Sub
 
Thanks JLGWhiz !

JLGWhiz said:
I forgot to select the range:

Sub fLastCell()
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox "Last Row Number is " & LastRow, , ""
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastCell = Cells(LastRow, LastColumn).Address 'Also add Address
Range("$A$1:" & LastCell).Select
End Sub
 

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