Help with Worksheet Deactivate

imback2nite

Board Regular
Joined
Oct 30, 2004
Messages
211
Office Version
  1. 2003 or older
Platform
  1. Windows
I'm having trouble with this code working as a deactivate. As of right now I have it working as a 'Update Name' Command Button which works fine but users are forgetting to 'Hit' the button and the names they are looking for are not showing up in the updated list. So, if I could have the list update when they leave the page or even a Worksheet Change that would be great. Thank you once again for helping!

Code:
Private Sub Worksheet_Deactivate()
    Application.ScreenUpdating = False  'Update Names'
    ActiveSheet.Unprotect "123"
    Columns("I:I").Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Columns("D:D").Select
    Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Range("E2:E500").ClearContents
    Range("E2") = "=IF(D2="""","""",COUNTIF(Galleys!$B$21:$AS$65,D2)+SUMPRODUCT((Galleys!$B$20:$AS$20=""LARGE"")*(Galleys!$B$21:$AS$65=D2)))"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E" & Cells(Rows.Count, "D").End(xlUp).Row)
    Sheet2.CommandButton1.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A2")
    Sheet2.CommandButton3.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A3")
    Sheet2.CommandButton4.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A4")
    Sheet2.CommandButton2.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A5")
    Sheet2.CommandButton6.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A6")
    Sheet2.CommandButton7.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A7")
    Sheet2.CommandButton11.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A8")
        Application.CutCopyMode = False
        ActiveSheet.Protect "123"
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
What is the problem?


In these lines you have to indicate on which sheet you want to work:

Code:
  ActiveSheet.Unprotect "123"
    Columns("I:I").Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Columns("D:D").Select
    Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Range("E2:E500").ClearContents
    Range("E2") = "=IF(D2="""","""",COUNTIF(Galleys!$B$21:$AS$65,D2)+SUMPRODUCT((Galleys!$B$20:$AS$20=""LARGE"")*(Galleys!$B$21:$AS$65=D2)))"
    Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E" & Cells(Rows.Count, "D").End(xlUp).Row)


----
Just as you did in the following lines, where you put the sheets.


Code:
    [COLOR=#0000ff]Sheet2[/COLOR].CommandButton1.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A2")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton3.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A3")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton4.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A4")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton2.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A5")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton6.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A6")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton7.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A7")
    [COLOR=#0000FF]Sheet2[/COLOR].CommandButton11.Caption = ThisWorkbook.Sheets("[COLOR=#006400]Disciplines[/COLOR]").Range("$A8")
 
Upvote 0
I keep getting stuck on this line.
Sheet5.Columns("I:I").Select
 
Upvote 0
Keep getting stuck on Sheet5.Range("I1:I500")
But I did change the ("I:I") to a specific parameter

Code:
Private Sub Worksheet_Deactivate()
    Application.ScreenUpdating = False  'Update Names'
    Sheet5.Unprotect "123"
    Sheet5.Range("I1:I500").Select
    Selection.Sort Key1:=Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Sheet5.Range("D1:D500").Select
    Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Sheet5.Range("E2:E500").ClearContents
    Sheet5.Range("E2") = "=IF(D2="""","""",COUNTIF(Galleys!$B$21:$AS$65,D2)+SUMPRODUCT((Galleys!$B$20:$AS$20=""LARGE"")*(Galleys!$B$21:$AS$65=D2)))"
    Sheet5.Range("E2").Select
    Selection.AutoFill Destination:=Range("E2:E" & Cells(Rows.Count, "D").End(xlUp).Row)
    Sheet2.CommandButton1.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A2")
    Sheet2.CommandButton3.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A3")
    Sheet2.CommandButton4.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A4")
    Sheet2.CommandButton2.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A5")
    Sheet2.CommandButton6.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A6")
    Sheet2.CommandButton7.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A7")
    Sheet2.CommandButton11.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A8")
        Application.CutCopyMode = False
        Sheet5.Protect "123"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
If you are on another sheet you cannot do this:
Code:
Sheet5.Range ("I1: I500"). Select
But I give you an option:
Try this

Code:
Private Sub Worksheet_Deactivate()
    Application.ScreenUpdating = False  'Update Names'
    Sheet5.Unprotect "123"
    Sheet5.Range("I1:I500").Sort Key1:=[COLOR=#0000ff]Sheet5[/COLOR].Range("I2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Sheet5.Range("D1:D500").Sort Key1:=[COLOR=#0000ff]Sheet5[/COLOR].Range("D2"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    Sheet5.Range("E2:E500").ClearContents
    Sheet5.Range("E2") = "=IF(D2="""","""",COUNTIF(Galleys!$B$21:$AS$65,D2)+SUMPRODUCT((Galleys!$B$20:$AS$20=""LARGE"")*(Galleys!$B$21:$AS$65=D2)))"
    Sheet5.Range("E2").AutoFill Destination:=[COLOR=#0000ff]Sheet5[/COLOR].Range("E2:E" & Cells(Rows.Count, "D").End(xlUp).Row)
    Sheet2.CommandButton1.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A2")
    Sheet2.CommandButton3.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A3")
    Sheet2.CommandButton4.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A4")
    Sheet2.CommandButton2.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A5")
    Sheet2.CommandButton6.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A6")
    Sheet2.CommandButton7.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A7")
    Sheet2.CommandButton11.Caption = ThisWorkbook.Sheets("Disciplines").Range("$A8")
    Application.CutCopyMode = False
    Sheet5.Protect "123"
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top