(Solved) – VBA Excel sort the data from A to Z in one column with all data table changes

  • by

It might be such a duplicate question with:

VBA Sort A-Z on One Column

However I want to have the stuff clarified.

I tried to use this code for my purpose:

Sub SortAsc2()

Dim LastRow As Long

LastRow = Cells(Rows.Count, “M”).End(xlUp).Row

  ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D"), _
  SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
   With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange Range("D2:D" & LastRow)
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
 End With

 End Sub

where I got an error:

Method ‘Range’ of object_Global failed

I tried another code then

  Sub SortDataWithoutHeader()
  Range("D1:D12").Sort Key1:=Range("D1"), Order1:=xlAscending, Header:=xlNo
  End Sub

But the sort happens only within the column, whereas the other data is unaffected.
I want to have values from other cells corresponding to the data sort.

enter image description here

Is anyone able to help?

Leave a Reply

Your email address will not be published. Required fields are marked *