Select entire Column till last row VBA

Wamhoi

New Member
Joined
Mar 4, 2011
Messages
48
I feel this is very simple fix but my code is flawed.

Dim LastRow As Long

With Worksheets("Sample")
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("R2:LastRow").Select
End With
 

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,)
Try this but it will error if Sample is not the active sheet

Code:
With Worksheets("Sample")
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    .Range("R2:R" & LastRow).Select
End With
 
Upvote 0
Wamhoi,

Try:


Code:
Option Explicit
Sub Test()

Dim LastRow As Long
With Worksheets("Sample")
  LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
  .Range("R2:R" & LastRow).Select
End With

End Sub
 
Upvote 0
Thanks so much, they worked out great!! For the sake of curiosity, what if you wanted to drag formulas from A2:D2 to last line?
 
Upvote 0
Something like this

Code:
With Worksheets("Sample")
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    .Range("R2").AutoFill Destination:=.Range("R2:R" & LastRow)
End With
 
Upvote 0
Something like this

Code:
With Worksheets("Sample")
    LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
    .Range("R2").AutoFill Destination:=.Range("R2:R" & LastRow)
End With

:confused: How do I use this code when the Worksheet changes name daily? (I download data from a SharePoint list and name the tab in yyyymmdd format, as well as the file itself in order to work with another script.) I want it to refer to the current active worksheet, whatever the name. Right now when I try to run the macro to select cell C1, go to the bottom of the column, and sort ascending, I get an "object error."

The code I'm using is:
Dim LastRow As Long
LastRow = .Cells(Rows.Count, "C").End(xlUp).Row.Range("C2:C) & LastRow).Select
Range("C1").Select
Range("C1:BK" & Range ("C1").End(xlDown).Row).Select
[And here's where it bombs]
Range("C1").CurrentRegion.Select.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

I believe there is redundancy in my Range commands too, but that doesn't seem to be the main issue.

Thanks.
 
Upvote 0
Try removing Select

Code:
Range("C1").CurrentRegion.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
 
Upvote 0
Wamhoi,

Try:


Code:
Option Explicit
Sub Test()

Dim LastRow As Long
With Worksheets("Sample")
  LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
  .Range("R2:R" & LastRow).Select
End With

End Sub

Hi,

In the above case how this code can be used to copy the contents what it is selected & paste it in different workbook.

Thanks,
Kit
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,712
Members
452,939
Latest member
WCrawford

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