Using formulas for sort fields in Excel VBA

Bunglesmate

New Member
Joined
Sep 24, 2012
Messages
2
Hello the team,

I am trying to use formulas in my vba code to generate sort fields.

I have a sheet which will have a column with a title "Use this to sort". The column might not be in the same place each time the macro is run.

I can use code to determine which column this is in but then how do I create a sort field using this variable?

Code:
[COLOR=#0000ff]'Determine the column which has "Use this to sort" as a header and set the column number[/COLOR]
[COLOR=#0000ff]
[/COLOR]
[COLOR=#0000ff]    Cells.Find(What:="Use this to sort", After:=ActiveCell, LookIn:= _[/COLOR]
[COLOR=#0000ff]        xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext _[/COLOR]
[COLOR=#0000ff]        , MatchCase:=False, SearchFormat:=False).Activate[/COLOR]
[COLOR=#0000ff]    [/COLOR]
[INDENT][COLOR=#0000ff]UseThisToSortCol = ActiveCell.Column[/COLOR][/INDENT]
The sort code generated from the macro recorder is as follows as I selected columns D & K to sort on manually.

Code:
[COLOR=#0000ff]ActiveSheet.Sort.SortFields.Clear[/COLOR]
[COLOR=#0000ff]    ActiveSheet.Sort.SortFields.Add Key:=Range( _[/COLOR]
[COLOR=#0000ff]    "D2:D10147"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _[/COLOR]
[COLOR=#0000ff]        xlSortNormal[/COLOR]
[COLOR=#0000ff]    ActiveSheet.Sort.SortFields.Add Key:=Range( _[/COLOR]
[COLOR=#0000ff]        "K2:K10147"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _[/COLOR]
[COLOR=#0000ff]        xlSortNormal[/COLOR]
[COLOR=#0000ff]    With ActiveSheet.Sort[/COLOR]
[COLOR=#0000ff]        .SetRange Range("A1:R10147")[/COLOR]
[COLOR=#0000ff]        .Header = xlYes[/COLOR]
[COLOR=#0000ff]        .MatchCase = False[/COLOR]
[COLOR=#0000ff]        .Orientation = xlTopToBottom[/COLOR]
[COLOR=#0000ff]        .SortMethod = xlPinYin[/COLOR]
[COLOR=#0000ff]        .Apply[/COLOR]
[COLOR=#0000ff]    End With[/COLOR]

What I'd like to do is use the variable 'UseThisToSortCol', and others to determine the sort column.

If I try to substitute the following

Code:
[COLOR=#0000FF]ActiveSheet.Sort.SortFields.Clear[/COLOR]
[COLOR=#0000FF]    ActiveSheet.Sort.SortFields.Add Key:=[/COLOR][COLOR=#ee82ee]Range( _[/COLOR]
[COLOR=#ee82ee]    "D2:D10147")[/COLOR][COLOR=#0000FF], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _[/COLOR]
[COLOR=#0000FF]        xlSortNormal[/COLOR]

with

Code:
[COLOR=#0000ff]ActiveSheet.Sort.SortFields.Clear[/COLOR]
[COLOR=#0000ff]    ActiveSheet.Sort.SortFields.Add Key:=[/COLOR][COLOR=#ee82ee]cells( _[/COLOR]
[COLOR=#ee82ee]        2, ComponentUnitCol:10147,ComponentUnitCol )[/COLOR][COLOR=#0000ff], SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _[/COLOR]
[COLOR=#0000ff]        xlSortNormal[/COLOR]

I get a compilation error.

Is this even possible?

All searches online so far, including MrExcel, do not seem to return the answers I am after. Maybe I am asking the wrong question.

Any help gratefully received.

Share & Enjoy.

Zippy.

¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬
Using MS Office Professional Plus 2010
Excel version 14.0.6123.5001 (32bit)
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello the team,

I have finally worked it out. Me muppet :eek:.

Thought I'd post it anyway in case anyone else was needing the answer.

Code:
ActiveSheet.Sort.SortFields.Clear
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
    [COLOR=#008000][B]"D2:D10147"[/B][/COLOR]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

Has been changed for the following


Code:
    ActiveSheet.Sort.SortFields.Add Key:=Range( _
    [B][COLOR=#008000]Cells(2, ComponentUnitCol), Cells(activeDatarowintab + 1, ComponentUnitCol)[/COLOR][/B]), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal

where ComponentUnitCol & activeDatarowintab are variables computed earlier in the macro.

Need to remember that the first one "D2:D10147" is in columnrow format whereas the second one is in row,column format.

Share & Enjoy.

Zippy.
 
Upvote 0

Forum statistics

Threads
1,216,735
Messages
6,132,423
Members
449,727
Latest member
Aby2024

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