ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Worksheet_Deactivate() problem with deleting data (https://www.excelbanter.com/excel-programming/340457-worksheet_deactivate-problem-deleting-data.html)

Karoo News

Worksheet_Deactivate() problem with deleting data
 

Hi I need to delete formaulas when a sheet deactivates but the formulas in
the next sheet thats acivated are deleted instead, is there a way to do
this? (the formaulas are copied in when the worksheets are activated)

Private Sub Worksheet_Deactivate()
ActiveSheet.Unprotect Password:="naWages"
Range("AG17:AH41").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("AY17:CI42").Select
Selection.ClearContents
Range("A1").Select
ActiveSheet.Protect Password:="naWages"
End Sub

Regards
Neil




Norman Jones

Worksheet_Deactivate() problem with deleting data
 
Hi Neil,

The following adaptation worked for me:

'====================
Private Sub Worksheet_Deactivate()

On Error GoTo XIT

Application.EnableEvents = False

With Me

.Unprotect Password:="naWages"

With .Range("AG17:AH41")
.Value = .Value
End With

.Range("AY17:CI42").ClearContents
.Protect Password:="naWages"

End With

XIT:
Application.EnableEvents = True

End Sub
'<<====================

BTW. apart from the fact that selections are rarely necessary, or desirable,
I would have expected your code to fail on the line:

Range("AG17:AH41").Select


given that a selection cannot be made on an inactive sheet.


---
Regards,
Norman



"Karoo News" wrote in message
...

Hi I need to delete formaulas when a sheet deactivates but the formulas in
the next sheet thats acivated are deleted instead, is there a way to do
this? (the formaulas are copied in when the worksheets are activated)

Private Sub Worksheet_Deactivate()
ActiveSheet.Unprotect Password:="naWages"
Range("AG17:AH41").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("AY17:CI42").Select
Selection.ClearContents
Range("A1").Select
ActiveSheet.Protect Password:="naWages"
End Sub

Regards
Neil





Tom Ogilvy

Worksheet_Deactivate() problem with deleting data
 
why not use the workbook level sheetdeactivate event. then you have a
reference to the sheet being deactivated. Remove all the selecting - since
the sheet is not the active sheet. write references to your locations

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
if sh.Name = "MySheet" then
with sh
.Unprotect Password:="naWages"
. Range("AG17:AH41").Formula = _
.Range("AG17:AH41:).Value
.Range("AY17:CI42").ClearContents
.Protect Password:="naWages"
End With
End if
End Sub

--
Regards,
Tom Ogilvy

"Karoo News" wrote in message
...

Hi I need to delete formaulas when a sheet deactivates but the formulas in
the next sheet thats acivated are deleted instead, is there a way to do
this? (the formaulas are copied in when the worksheets are activated)

Private Sub Worksheet_Deactivate()
ActiveSheet.Unprotect Password:="naWages"
Range("AG17:AH41").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("AY17:CI42").Select
Selection.ClearContents
Range("A1").Select
ActiveSheet.Protect Password:="naWages"
End Sub

Regards
Neil






Karoo News

Worksheet_Deactivate() problem with deleting data
 
That worked fantastic - many thanks - Neil

"Norman Jones" wrote in message
...
Hi Neil,

The following adaptation worked for me:

'====================
Private Sub Worksheet_Deactivate()

On Error GoTo XIT

Application.EnableEvents = False

With Me

.Unprotect Password:="naWages"

With .Range("AG17:AH41")
.Value = .Value
End With

.Range("AY17:CI42").ClearContents
.Protect Password:="naWages"

End With

XIT:
Application.EnableEvents = True

End Sub
'<<====================

BTW. apart from the fact that selections are rarely necessary, or
desirable, I would have expected your code to fail on the line:

Range("AG17:AH41").Select


given that a selection cannot be made on an inactive sheet.


---
Regards,
Norman



"Karoo News" wrote in message
...

Hi I need to delete formaulas when a sheet deactivates but the formulas
in the next sheet thats acivated are deleted instead, is there a way to
do this? (the formaulas are copied in when the worksheets are activated)

Private Sub Worksheet_Deactivate()
ActiveSheet.Unprotect Password:="naWages"
Range("AG17:AH41").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("AY17:CI42").Select
Selection.ClearContents
Range("A1").Select
ActiveSheet.Protect Password:="naWages"
End Sub

Regards
Neil








All times are GMT +1. The time now is 12:42 PM.

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