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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

Forum statistics

Threads
1,215,504
Messages
6,125,183
Members
449,212
Latest member
kenmaldonado

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