Trying to modify code to run when user leaves a sheet

Maniacal

New Member
Joined
Mar 1, 2019
Messages
2
Hi all,

Apologies, I've been doing some reading and little out of my depth. I'm trying to convert the below code to work with worksheet_deactivate() command (when leaving a worksheet "Rebate Conditions"), however not having any luck. It works as a macro in a button on the sheet, just not when leaving the sheet so would assume is has to do with an activesheet issue.

I've read that the 1004 error will be caused by range issues in the code and tried adding in ME. and replacing activesheet with thisworkbook.sheets("rebate conditions") with no success.

Hoping someone may be able to steer me right.

Code:
Sub Refresh_Rebates()Dim RCount As Integer


'Optimize Code
  Call OptimizeCode_Begin


Rows.EntireRow.Hidden = False
RCount = Range("D" & ActiveSheet.Rows.Count).End(xlUp).Row


Range("B4:R" & RCount).Sort key1:=Range("E5:E" & RCount), Order1:=xlDescending, Header:=xlYes
Range("G2").Select
Selection.Copy
Range("H2").Select
ActiveSheet.Paste


Rows.EntireRow.Hidden = False
RCount = Range("B" & ActiveSheet.Rows.Count).End(xlUp).Row
For i = 5 To RCount
    If Range("D" & i).Value = 0 Then
        Range("J" & i).Value = ""
        Range("M" & i).Value = ""
        Range("P" & i).Value = ""
    End If
Next


'Optimize Code
  Call OptimizeCode_End
  
   MsgBox "Data updated"


End Sub
G2 is a fixed value. H2 is a variable.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Presuming your code is as you want you just need to qualify those ranges with a sheet name.

Code:
Private Sub Worksheet_Deactivate()

Dim RCount As Integer

'Optimize Code
Call OptimizeCode_Begin

With Sheets("Rebate Conditions")
    .Rows.EntireRow.Hidden = False
    RCount = .Range("D" & .Rows.Count).End(xlUp).Row
    .Range("B4:R" & RCount).Sort key1:=.Range("E5:E" & RCount), Order1:=xlDescending, Header:=xlYes
    .Range("G2").Copy .Range("H2")
    RCount = .Range("B" & .Rows.Count).End(xlUp).Row
    For i = 5 To RCount
        If .Range("D" & i).Value = 0 Then
            .Range("J" & i).Value = ""
            .Range("M" & i).Value = ""
            .Range("P" & i).Value = ""
        End If
    Next
End With

'Optimize Code
Call OptimizeCode_End

MsgBox "Data updated"

End Sub

Not quite sure what you mean by G2 is a fixed value and H2 is a variable?
 
Upvote 0
Code:
Private Sub Worksheet_Deactivate()
Refresh_Rebates
End Sub
 
Upvote 0
Presuming your code is as you want you just need to qualify those ranges with a sheet name.

Code:
Private Sub Worksheet_Deactivate()

Dim RCount As Integer

'Optimize Code
Call OptimizeCode_Begin

With Sheets("Rebate Conditions")
    .Rows.EntireRow.Hidden = False
    RCount = .Range("D" & .Rows.Count).End(xlUp).Row
    .Range("B4:R" & RCount).Sort key1:=.Range("E5:E" & RCount), Order1:=xlDescending, Header:=xlYes
    .Range("G2").Copy .Range("H2")
    RCount = .Range("B" & .Rows.Count).End(xlUp).Row
    For i = 5 To RCount
        If .Range("D" & i).Value = 0 Then
            .Range("J" & i).Value = ""
            .Range("M" & i).Value = ""
            .Range("P" & i).Value = ""
        End If
    Next
End With

'Optimize Code
Call OptimizeCode_End

MsgBox "Data updated"

End Sub

Not quite sure what you mean by G2 is a fixed value and H2 is a variable?

Thank you so so much. Works like a charm.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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