ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   How to create a macro that sorts data when I click save? (https://www.excelbanter.com/excel-discussion-misc-queries/209632-how-create-macro-sorts-data-when-i-click-save.html)

Abdul

How to create a macro that sorts data when I click save?
 
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.


Madiya

How to create a macro that sorts data when I click save?
 
On Nov 10, 8:53*am, Abdul wrote:
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

Shane Devenshire

How to create a macro that sorts data when I click save?
 
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.

"Abdul" wrote in message
...
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

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


"Madiya" wrote:
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.


All times are GMT +1. The time now is 10:02 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com