Simplify code before close

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I assume the code supplied below is what you require ?

When i close the sheet i see the below code running & each page is flickered for me to view.
Can it be simplified or changed so when i click close it then does its thing without the visual effect that i currently see each time ?


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)Sheets("Sheet1").Range("P1:U1").Copy Sheets("Sheet1").Range("E6")
Sheets("Sheet1").Range("P2").Copy Sheets("Sheet1").Range("E7")


Sheets("DR SITE").Activate
ActiveSheet.Range("E6:J6").ClearContents
Range("E7").Select


Sheets("EBAY").Activate
ActiveSheet.Range("E6:J6").ClearContents
Range("E7").Select


Sheets("DR SITE").Activate
ActiveSheet.Range("E7").ClearContents
Range("E7").Select


Sheets("EBAY").Activate
ActiveSheet.Range("E7").ClearContents
Range("E7").Select


ActiveWorkbook.Save
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
hows this for a start
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False
Sheets("Sheet1").Range("P1:U1").Copy Sheets("Sheet1").Range("E6")
Sheets("Sheet1").Range("P2").Copy Sheets("Sheet1").Range("E7")

Sheets("DR SITE").Range("E6:J6").ClearContents

Sheets("EBAY").Range("E6:J6").ClearContents

Sheets("DR SITE").Range("E7").ClearContents

Sheets("EBAY").Range("E7").ClearContents
Application.ScreenUpdating = True
ActiveWorkbook.Save
End Sub
 
Upvote 0
Perfect.

was it the screen update part that causes the issue ?
 
Upvote 0
Perfect.

was it the screen update part that causes the issue ?
Actually, it was your activating each sheet before clearing the cells on that sheet. If you look at mole999's code, you will see that he simply clears the cells by referencing the sheet directly... doing it this way stops the visual skipping from sheet to sheet (activating a sheet displays that sheet, referencing it does not). By the way, since mole999's code does not activate any other sheets, you can probably remove the two Application.ScreenUpdating code lines without any noticeable affect.
 
Upvote 0
Or if you want to select E7 on the 2 sheets before closing :
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.ScreenUpdating = False
Sheets("Sheet1").Range("P1:U1").Copy Sheets("Sheet1").Range("E6")
Sheets("Sheet1").Range("P2").Copy Sheets("Sheet1").Range("E7")


Sheets("DR SITE").Activate
Range("E6:J6,E7").ClearContents
Range("E7").Select


Sheets("EBAY").Activate
Range("E6:J6,E7").ClearContents
Range("E7").Select


ActiveWorkbook.Save
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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