ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Worksheet Functions (https://www.excelbanter.com/excel-worksheet-functions/)
-   -   Force sort before worksheet exit (https://www.excelbanter.com/excel-worksheet-functions/137739-force-sort-before-worksheet-exit.html)

Kathrine

Force sort before worksheet exit
 
I have a list of data that I want to ensure is sorted when the user exits
from the worksheet.

Today I have a button with a macro attached to it (sort_data). But the user
will have to actually push the button. If he forgets there is a risk that
functions like INDEX in other sheets won't work properly....

Can I use VBA to detect worksheet exit and trigger the sort macro before
leaving?

Gary''s Student

Force sort before worksheet exit
 
In Worksheet code put:

Private Sub Worksheet_Deactivate()
Call sort_data
End Sub

This will run the sort routine before exiting the worksheet.

REMEMBER: Worksheet code.
--
Gary''s Student - gsnu200713

Kathrine

Force sort before worksheet exit
 
Thank you for helping me again.
However, I still can't make it work. I had in fact already tried what you
suggested but I'm getting an error message

Runtime error 1004: Sort method in range class failed (translated from
Norwegian)

It seems that in worksheet.deactivate the worksheet that I exit from and
need to sort is no longer the active worksheet and thats why the sort won't
work....? Here is my sort macro as it is used when invoked from the button.

Sub Sorter_klasser()
'
' Sorter_klasser Makro
' Makro registrert 02.04.2007 av Kathrine S Ekrem
'
ActiveSheet.Unprotect
Range("Klassematrise").Sort Key1:=Range("A8"), Order1:=xlAscending,
Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Range("A7").Select
ActiveSheet.Protect
End Sub


Perhaps this macro needs to be changed when invoked from
worksheet.deactivate...? Any clues.?


"Gary''s Student" wrote:

In Worksheet code put:

Private Sub Worksheet_Deactivate()
Call sort_data
End Sub

This will run the sort routine before exiting the worksheet.

REMEMBER: Worksheet code.
--
Gary''s Student - gsnu200713



All times are GMT +1. The time now is 07:15 PM.

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