Trying to clear column when workbook closes


I have a spreadsheet that I use to check on my credit card balances.  I use a simple "x" in one of the columns to denote that it's been checked.  I'd like to clear this column every time I close the workbook.  I have the attached code in VBA, but it doesn't clear the column when I close the file.

BBluAsked:
Who is Participating?
 
patrickabCommented:
Perhaps like this:

Sub ClearCheckCol()
    Sheets("Sheet1").Columns("G:G").ClearContents
'or specify whichever sheet is correct
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.EnableEvents = True
    Call ClearCheckCol
End Sub
0
 
BBluAuthor Commented:
oops.  Here's the code I'm trying to use.
Sub ClearCheckCol()
'
' ClearCheckCol Macro
'

'
    Columns("G:G").Select
    Selection.ClearContents
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call ClearCheckCol
End Sub

Open in new window

0
 
patrickabCommented:
BBlu,

>I have the attached code in VBA, but it doesn't clear the column when I close the file.

Please let us see the VBA code - thanks.

Patrick
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Patrick MatthewsCommented:
That looks like it ought to work.  Three thoughts:

1) Are you sure macros are enabled?

2) If yes, are you sure that events are enabled?

3) If yes, are you sure that the ClearCheckCol sub is operating against the right worksheet?  Since you are not qualifying the range reference, it is acting against the current active sheet.  You should probably qualify the reference
0
 
patrickabCommented:
BBlu,

>You should probably qualify the reference - as per Patrick Matthews' comment...

Sub ClearCheckCol()
    Sheets("Sheet1").Columns("G:G").ClearContents
'or specify whichever sheet is correct
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call ClearCheckCol
End Sub

Patrick(ab) - a different Patrick!
0
 
BBluAuthor Commented:
I changed the code per Patrick's thoughts.  Still not working.  I have macros enabled.  But I don't know how to enable events.
0
 
Patrick MatthewsCommented:
Modify your event sub to read:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    MsgBox "foo!"
    Call ClearCheckCol
End Sub

Open in new window


If you try to close the workbook, do you get that message?

Also, are you certain that you placed the Workbook_BeforeClose sub in the ThisWorkbook module for the specific workbook you want the code to operate on?  Putting it in a different module, or even a different workbook (such as an add-in or a personal macro workbook) would be a problem :)

I'd also extend the qualification to the workbook:

Sub ClearCheckCol()
    ThisWorkbook.Worksheets("Sheet1").Columns("G:G").ClearContents
'or specify whichever sheet is correct
End Sub

Open in new window

0
 
Patrick MatthewsCommented:
Patrick,

If events aren't enabled, the BeforeClose event won't fire, and thus events won't get enabled that way :)

Patrick
0
 
BBluAuthor Commented:
You are funny, Matthew.  No, I don't get that message.  How do I enable the events?
0
 
Patrick MatthewsCommented:
Please state exactly where you placed the Workbook_BeforeClose sub.
0
 
BBluAuthor Commented:
I put it in a module for the Workbook.  See attached. Close Event VBA Screenshot
0
 
Patrick MatthewsCommented:
Code for workbook events belongs in the ThisWorkbook module, not in a "regular" module.
0
 
BBluAuthor Commented:
Thanks to you both . The code provided worked...as long as I put it in the right darn location.  LOL
0
 
BBluAuthor Commented:
Thanks, again, Guys.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.