Syntax using variables in range address

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
336
Office Version
  1. 2019
I'm familiar with syntax such as the following, using one variable in the address:
VBA Code:
Range("A4", "E" & LastRow)

But I can't work out the correct syntax for something using two variables, like:
VBA Code:
ActiveSheet.Range("A3", LastColumn & LastRow).Select

Can someone point me to the correct syntax please?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
try
VBA Code:
ActiveSheet.Range(Cells(3, 1), Cells(LastColumn, LastRow)).Select
 
Upvote 0
OOps...wrong way round
AND why are you selecting them, probably no need to ??
VBA Code:
ActiveSheet.Range(Cells(3, 1), Cells(LastRow, LastColumn)).Select
 
Upvote 0
Solution
Thanks Michael. The next step in the code is to sort the data according to the date in column B. For that, I'm using code generated by the macro recorder, which starts with a selection. I'm by no means proficient in VBA - I just cobble together bits of code I come across to grope my way to a solution, ha!
 
Upvote 0
If the code created by the recorder works, post it here and someone may be able to shorten it for you !
 
Upvote 0
I did a bit of critical analysis on the code and saw where there was some macro-recorder-generated redundancy. My full code, which works is now:
VBA Code:
Sub DateSorting()

Dim LastRow As Long, LastColumn As Long

LastRow = ActiveSheet.UsedRange.Rows.Count
LastColumn = ActiveSheet.Cells(LastRow, Columns.Count).End(xlToLeft).Column

ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range("B3"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortTextAsNumbers
    With ActiveSheet.Sort
        .SetRange Range(Cells(3, 1), Cells(LastRow, LastColumn))
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

End Sub
 
Upvote 0
that looks OK...and will allow for dynamic ranges of cells
 
Upvote 0

Forum statistics

Threads
1,215,053
Messages
6,122,882
Members
449,097
Latest member
dbomb1414

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