How to Unsort the Sorted Data ( Excel Vba)

I'm now sorting some data using a VBA code. ( for some search feature)

After my search is complete I would like to unsort the data back into its original form. How do I do that?

Thanks.

3

4 Answers

Just make a copy of the data before sort and set it back..
Other way: you need preserve keys before sort (with hash map) and resort array (after main sort) again by those keys.

I am not sure how to do it using VBA coding. However, you've two options provided you haven't saved changes, if any:

  1. Before sorting add a column with numbers running from 1. Then if you want to restore your sort, you can then sort on this column.
  2. In MS Excel 2010 version there is an option to unsort multiple/many columns in excel in one go:

    HOME->EDITING->SORT &FILTER drop down menu->CLEAR.

This will clear all the columns/rows in the that Worksheet.

Ctrl+Z undoes the action if you have not saved it. Another option is to create another sheet and paste the data in there to use as a backup.

This is something I had to resolve of late as well. This is how I did it.

  1. Create a temporary index (just numbers 1..n) in the last sortcolumn + 1
  2. Include the temporary index column in the sort
  3. Do sorting and processing
  4. Sort using the temporary index
  5. Clear/delete the temporary index column

Hope this helps someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest