Add level to sort data in code

  • Hello experts

    I am facing a very weird problem. I have this code which is running fine and giving the correct result. I just want to add a level to sort the data in the code. The weird part is that the old code is working fine but after adding another level to sort it is wiping out the whole data. I need your expertise to help me to help me find and correct the issue.

    Code
     Range("A2:" & LastColumn & Range("A" & Rows.Count).End(xlUp).Row).Sort _
                Key1:=Range(DestinationRemarksColumn & "2"), Order1:=xlAscending, Header:=xlNo                          ' Sort Remarks Column lowest to highest

    This code has one level to sort and is working correct.


    Code
    Range("A2:" & LastColumn & Range("A" & Rows.Count).End(xlUp).Row).Sort _
                Key1:=Range("C2"), Order1:=xlAscending, _
                Key2:=Range(DestinationRemarksColumn & "2"), Order1:=xlAscending, Header:=xlNo  

    After replacing the above lines with the below lines the code is wiping the data.

    Weird as it sounds but this is the issue I am facing.

    to add level to sort.xlsm

  • Go to Best Answer
  • Hello,


    With Key1 you have Order1 .... but with Key2, you should have Order2 ...


    Hope this will help

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Hello,


    With Key1 you have Order1 .... but with Key2, you should have Order2 ...


    Hope this will help

    I replaced Order1 to order2 but it is still deleting the whole data.

    If you comment the 3 new added lines and uncomment the 2 commented lines then the code is running correct and not deleting the data.

  • Sorry but in order to test your file would need to need which macro in which module .... and which worksheet you are referring to ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • The "A4_Copy_Rename_MatchedSheet" module is sorting the data. The sheet created by the code "to correct Invoice No. & Date" is the sheet to be sorted with an additional level.

  • Once the code has finished displaying the result, I can manually sort the data in "to correct Invoice No. & Date" sheet and get the correct result. But I want to include those steps in the code.

  • Hello again,


    Just tested your workbook and the sort macro which does operate fine ...


    Your problem is created to the instruction which follows :

    Code
    Rows(FirstMatch & ":" & LastRow).Delete

    Just comment it out .... and everything looks fine ...

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

  • Modified your code as follows:-


    • Best Answer

    Here is a shorter version of your macro


  • Thanks HolyCow. The shorter version is working and giving me the expected result. Without comments I am completely lost. If you could PLEASE edit and correct my code I would really appreciate it.

  • Thanks Carim. But I have to delete the Matched rows in column Invoice value to know which all transactions need to be corrected. Now that the problem is resolved, thanks a lot for your support. :)

  • Pleased to hear it is all sorted out :)

    If you feel like saying "Thank You" for the help received, do not hesitate to click the "Smiley" icon, below, in the bottom right corner :)

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!