Search code examples
vbaexcelhta

Running vba to sort excel from HTA


How do I use below code recorded from excel Marco recorder in HTA?

Sub Macro1()
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:= _
        Range("M2:M9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:= _
        Range("B2:B9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:M9")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

I can define the range, sheet names etc.. But not able to set the properties to sort:(

Thanks, Anand


Solution

  • Set objRange1 = objExcel.Range("M1") Set objRange2 = objExcel.Range("A1") objRange.Sort objRange1, xlAscending, objRange2, , xlAscending, , , xlYes