Macro to clear data on close of workbook

pedro-egoli

Well-known Member
Joined
Apr 25, 2004
Messages
1,217
Office Version
  1. 2016
Platform
  1. Windows
Just need some assistance to tidy up this macro , which is intended to clear data in cells B4:B7 in worksheet "test".
My code below simply leaves old data and places new data in adjoining column.
VBA Code:
Sub Centrelink()

Sheets("accountbalance").Range("d9:d12").Copy
Sheets("test").Cells(4, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
        Application.CutCopyMode = False
    Selection.NumberFormat = "$#,##0.00"
    Selection.NumberFormat = "$#,##0.0"
    Selection.NumberFormat = "$#,##0"
End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Updated by Extendoffice 20190712
    Worksheets("test").Range("B4:B7").Value = ""

End Sub
Any assistance appreciated , as I have combined a couple of codes , one which was obtained here and one from the net
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Two things:

1. You want to make sure that you save your workbook after clearing the range. So, try updating the code like this:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Updated by Extendoffice 20190712
    Worksheets("test").Range("B4:B7").Value = ""
    ActiveWorkbook.Save
End Sub

2. Make sure that the above code is put in the "ThisWorkbook" module in VBA. If you put it in any other module, it will NOT run automatically.
 
Upvote 0
Thanks for that won't have access to computer until tomorrow so will let you know how it goes then .
Thanks again
 
Upvote 0
I need a hand here.
Added that line as suggested but data still remains when workbook is closed.
Currently VBA Project for workbook shows
“Microsoft Visual Basic Applications -CGT IMPLICATIONS SALE OF SHARES 08092020.XLSM-[Shee3(Code)]”

And page with Object being “General” and Procedure being “centrelink”
Under the Microsoft Excel objects are 7 sheets with tab title and ”this workbook”
Below that is Modules with one module containing another macro.
When I right click sheet 3 “accountbalance” the code appears
Initially I had endeavoured to use the macro entitled “centrelink” which I use on a completely different workbook however, the macro on sheet 3 is named “Sheet3.centrelink” and as I said before this runs the macro but does not clear data on close

I note that you said to place code in “this workbook” and I want to know what procedure should be to get to this bearing in mind what I have already done.
 
Upvote 0
Thanks for the link.
I got it to work with this in Thisworkbook
VBA Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)

End Sub
Sub Centrelink()

Sheets("accountbalance").Range("d9:d12").Copy
Sheets("test").Cells(4, Columns.Count).End(xlToLeft).Offset(, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
        Application.CutCopyMode = False
    Selection.NumberFormat = "$#,##0.00"
    Selection.NumberFormat = "$#,##0.0"
    Selection.NumberFormat = "$#,##0"
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
'Updated by Extendoffice 20190712
    Worksheets("test").Range("B4:B7").Value = ""
ActiveWorkbook.Save

End Sub



Private Sub Workbook_Open()

End Sub
Thanks again
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,993
Members
448,539
Latest member
alex78

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