

#Bbedit sort functions code
What if you have data which you need to sort based on multiple columns? Is it possible to write a code for the same?
#Bbedit sort functions how to
Till now, we have covered how to sort a single column data in ascending or descending order (without header and with header). You can see that the data is sorted in Descending order. Step 7 : Run the code and see an output as given below. Sheets("Example #2").Range("A1").Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlYes Step 6 : Change order1 to descending in order to sort the data in descending order. All you need to do is change the order from ascending to descending. You can also sort the same data in descending order of alphabets. Which means that, while sorting this data, first row (which contains Emp Name) is neglected because it is considered as a header for this data in column A. Here, the data from Example #2 of given excel workbook is sorted in ascending order given that it has a header. Step 5 : Run this code by hitting F5 or Run button manually and See the output. Sheets("Example #2").Range("A1").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes Step 4 : Provide Key1 as Range(“A1”) to sort the data from cell A1, Order1 in order to sort the data in ascending or descending order and Header as Yes in order to let the system know that first row is header in your data. Step 3 : Use Range(“A1”).Sort in front of the above code line to make it a sort function.

Step 2: Use Sheets(“Example #2”) to specify which sheet your data is. Step 1: Define a new sub-procedure under a new model to store the macro. And you wanted to sort this column in ascending or descending order. Suppose you have a column with the header as shown below. If you can compare this with the image at the start of this example, you can see that names are sorted in ascending order. Step 4: Run this code by hitting F5 or Run button manually and see the output. This code will check for all non-empty cells starting from A1 and then sort them in ascending order starting from cell A1. Order is provided as Ascending and Header as No (Which means column doesn’t have header). We have given Range(“A1”) as the column starting range which we need to sort. Range("A1", Range("A1").End(xlDown)).Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNoĪs we have discussed earlier, Key, Order, and Header are the important and necessary arguments to be provided. Here you are giving range starting from cell A1 to the last used/non-empty cell (see the function. Step 2: Use Range.Sort function to be able to sort this column in ascending order. Step 1: Define a new sup-procedure under a module and create a macro. Follow the below steps to use Sort function in VBA.
