0

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

5
  • 1
    What have you tried, and what happened? It would be useful to add your current (vbscript? javascript?) code, and describe what errors you're getting. To start, you cannot use Excel-specific constants such as xlYes without either defining them in your HTA script code or replacing them with their actual values (which you can find via the Excel VB editor Object Browser) Jul 14, 2014 at 2:15
  • The error says invalid property assignments or invalid data
    – Anand
    Jul 14, 2014 at 3:04
  • Use breakpoints and find which line the error occurs in
    – hnk
    Jul 14, 2014 at 4:33
  • I think all I need to know is how to put the line ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:= _ Range("M2:M9"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal in HTA
    – Anand
    Jul 14, 2014 at 17:00
  • Fixed it.. Set objRange1 = objExcel.Range("M1") Set objRange2 = objExcel.Range("A1") objRange.Sort objRange1, xlAscending, objRange2, , xlAscending, , , xlYes
    – Anand
    Jul 14, 2014 at 23:52

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.