Data Sort based off of columns numbers stored in varibales

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Ok, so here is a fun one. My boss gave me the task of writing a macro that needs to be completely dynamic. The problem I have run into is when I want to sort the data on a sheet I need the sort procedure to be able to sort based of stored values instead of like "B2","B20".

What I have right now is:
Code:
Dim FirstCellSort As String
FirstCellSort = CallTypeColumn & ",2"
Dim LastCellSort As String
LastCellSort = CallTypeColumn & "," & LastCell

        ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Raw Data").Sort.SortFields.Add Key:=Range(FirstCellSort, LastCellSort _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
........
The variables have the following stored values:
FirstCellSort = "4,2"
LastCellSort = "4,20"

This does not work I believe because I am not using a reference like "D2" but instead referencing it as "4,2"

Can anyone help me out?! :confused:
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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