Wednesday, August 8, 2018

Sort an user defined portion of excel table or range or data using VBA,VBA Teacher Sourav,Kolkata 8910141720


Sub Macro1()
'
' Macro1 Macro
'

'
Dim selectrange As Range
Dim sorton As Range
Set selectrange = Application.InputBox(prompt:="select the cells for the range", Type:=8)

Set sorton = Application.InputBox(prompt:="select the cells for the range to be sorted", Type:=8)
  
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range(sorton.Address) _
        , sorton:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range(selectrange.Address)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub

No comments:

Post a Comment