Home > database >  Assign variable for CustomOrder parameter in vba
Assign variable for CustomOrder parameter in vba

Time:04-14

I have the following code

Sub Test()
    Dim x
    x = "ow, bv, xz"
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("D1"), CustomOrder:=x
        .SortFields.Add Key:=Range("C1"), Order:=xlDescending
        .SortFields.Add Key:=Range("B1"), Order:=xlAscending
        .SetRange Range("A1").CurrentRegion
        .Header = xlYes
        .Apply
    End With
End Sub

I tried to use CustomOrder and assign a variable x to the string. When using the string directly, it works well but not with using variable Any idea how to fix that?

CodePudding user response:

Seems like a bug. The following work for me:

CustomOrder:=Cstr(x)
CustomOrder:=Join(Split(x)) 'overkill, but for laughs
CustomOrder:=x & vbNullString
  • Related