Solved

Trying to clear column when workbook closes

Posted on 2011-02-21
14
216 Views
Last Modified: 2012-05-11

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.

0
Comment
Question by:BBlu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 

Author Comment

by:BBlu
ID: 34945750
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
 
LVL 45

Expert Comment

by:patrickab
ID: 34945762
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34945930
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:patrickab
ID: 34945992
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
 

Author Comment

by:BBlu
ID: 34946072
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34946148
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
 
LVL 45

Accepted Solution

by:
patrickab earned 150 total points
ID: 34946164
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
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34946174
Patrick,

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

Patrick
0
 

Author Comment

by:BBlu
ID: 34946358
You are funny, Matthew.  No, I don't get that message.  How do I enable the events?
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34946417
Please state exactly where you placed the Workbook_BeforeClose sub.
0
 

Author Comment

by:BBlu
ID: 34947408
I put it in a module for the Workbook.  See attached. Close Event VBA Screenshot
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 50 total points
ID: 34948896
Code for workbook events belongs in the ThisWorkbook module, not in a "regular" module.
0
 

Author Comment

by:BBlu
ID: 34974089
Thanks to you both . The code provided worked...as long as I put it in the right darn location.  LOL
0
 

Author Closing Comment

by:BBlu
ID: 34974125
Thanks, again, Guys.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help on Solver add-in 30 45
NEED TO LOOK FOR EXACT NUMBER 14 40
NEED LOOK FOR NUM 23 25
format is changing when i run the find and replace macro 8 17
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question