How to create a macro that sorts data when I click save?

A

Abdul

I need help creating a macro that sorts the data in sheet#2 at cell B1 as
Descending "from Z to A" when I click on Save.
normally, I only click save when I'm done with the file, so it doesn't
matter if the macro activates only if I click save, but something similar
also works.
because sorted data doesn't update when the values change for what ever
reason, I have to click on Sort Descending each time I update the data. and I
want to automate it using a macro.
I have posted earlier a question "why Sort Descending does not refresh?" and
I got an answer that I can do that using a macro, but I don't have skill in
VB to do it myself. please assist.
 
M

Madiya

I need help creating a macro that sorts the data in sheet#2 at cell B1 as
Descending "from Z to A" when I click on Save.
normally, I only click save when I'm done with the file, so it doesn't
matter if the macro activates only if I click save, but something similar
also works.
because sorted data doesn't update when the values change for what ever
reason, I have to click on Sort Descending each time I update the data. and I
want to automate it using a macro.
I have posted earlier a question "why Sort Descending does not refresh?" and
I got an answer that I can do that using a macro, but I don't have skill in
VB to do it myself. please assist.

Abdul,
You have posted in wrong group.
But here is your macro.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Sheets("SHEET2").Select
Range("B1").Select
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending,
Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom,
_
DataOption1:=xlSortNormal

End Sub

copy paste above macro in thisworkbook in your VBA editor.

Also try to use macro recorder which will surely help you to get
started.

Hope this helps.

Regards,
Madiya
 
S

Shane Devenshire

Hi,

1. Record the sort command
2. Add the code to the Workbook_BeforeSave event.

You can find this event in the VBE by double-clicking the thisWorkbook
object in the Projects window and pick Workbook from the Object dropdown at
the top left in the code window, and BeforeSave from the procedure drop down
at the top right of the code window. Paste your code into this procedure.

If this helps, please click the Yes button.
 
A

Abdul

Madiya said:
Abdul,
You have posted in wrong group.

I'm new to this community, please redirect me to where I should've posted
this, but thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top