Clearing status cells - Excel Macros

nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

It can only be one of two things really

Either you have no formulas in your worksheet (unlikely), or it's the conditions not being met that's stopping it.

Put this in

Private Sub Worksheet_Calculate()
Dim i As Integer

For i = 9 To 67 Step 2

Range("O" & i).ClearContents
Next i

End Sub

now put anything in O9 and then in another cell put a calculation: =2 will do

O9 should now be clear?
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

Hi nigelk,

Thanks for taking the time helping me out with this.

I did what you recommended with the same effect... it works as expected if I hitthe "play" button from the VBA interface, but doesn't automatically behave like that without my interaction.

The condition are met as the only condition I have is O9, O11, etc having the word "FAILED" which it clears correctly when run manually...

Pretty weird thing. I'm using Excel 2010 and I've put this in a book with macros enabled. Any other idea on how to determine where this may be coming from?

Thank you. Cheers!
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

It is the sheet, not a module?
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

nigelk wrote:It is the sheet, not a module?
Yep! I've put it in the exact sheet, in the Worksheet... nothing happens automatically.

I've tried to look for anything regarding automatically running macros, but it all seems normal.

Thanks a lot!
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

I really don't get it...

After thinking for a while I thought it could be an issue with the security settings for macros. I went to the Excel's "Trust Center" and enabled everything (which I know is a risk). Still... all goes fine when run manually but it does nothing if I write on a cell it should automatically clear.

I hate these things! It has to be a silly one but I can't find it!

Just needed to speak out the desperation... hehe!
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

Have you got a) any disable events in your routine?
b) any error trapping routines that have
not been re-set.
c) does it call any other routines?
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

To summarize the exact steps of what I'm doing:
1. Create a new Excel workbook
2. Press Alt+F11 to enter VBA.
3. Right click on the name of my sheet -> view code
4. Paste the code you put.
5. Save it.
6. Close and reopen the workbook (just in case...).
7. Write something in the fields from O9 to O17: nothing happens.
8. Enter VBA and manually run de macro: it clears out the O9 - O17 cells as expected.

Maybe I need to trigger an event for the macro to run... or should it automatically run everytime a cell is changed as I expect?

Thank you so much!
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

There are two reasons why it won't work.

It's a Calculate event not a change event, so putting anything a cell won't cause it to fire. It has to be preceded with an = sign.

Also the cells in question are formatted as text, so even if you do put a calculation in there, nothing will happen.

Put something in O9, and then in M9 put =K9, the O9 will clear.

NB. Formatting is generally for our benefit only,if you need a date who wants to see 40658 in a cell? Much better if we see 25/04/11. If a cell is formatted as text Excel will treat it as, well text, and not a number or calculation to be acted upon.
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

nigelk wrote:There are two reasons why it won't work.

It's a Calculate event not a change event, so putting anything a cell won't cause it to fire. It has to be preceded with an = sign.

Also the cells in question are formatted as text, so even if you do put a calculation in there, nothing will happen.

Put something in O9, and then in M9 put =K9, the O9 will clear.

NB. Formatting is generally for our benefit only,if you need a date who wants to see 40658 in a cell? Much better if we see 25/04/11. If a cell is formatted as text Excel will treat it as, well text, and not a number or calculation to be acted upon.
Ooooooh god, it's finally working!

I didn't get that it was a calculate event only. I have the cells as "General".

My question is... will this be triggered by changes made in the sheet by BetAngel? Because the end of all this was to clear the specific cells when they showed "FAILED" or "ERROR" in order to make sure it places a bet.

Thanks a lot for your infinite patience nigelk!

P.S: I won't like to try it out with a real situation as I don't want it to start firing lots of bets without control.
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

The easiest way to find out is to put this line in at the beginning or end

Msgbox "Calculation"

Then you'll see if a calculation been carried out. Obviously, take it when you are happy with what you are trying to do.
JacoboPolavieja
Posts: 91
Joined: Thu Sep 10, 2009 6:00 pm

It is indeed a calculation.. thank God I finally see the lght at the end of the tunnel! Well... and thanks a lot to you nigelk :D.

There's just one step left I think which is that I have several sheets. Just guessing... as the code works on a per sheet basis, is there any problem just copying the sheet I have finally working to have several ones?
Maybe there's a performance issue or another issue I haven't thought about. After all the struggle maybe I'm just too worried and looking for too many problems.

Thank you very much for all the help nigelk, you can't know how much you've helped me, but you will sure have a free beer if you ever come to Spain ;).

Thanks! Cheers!
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

Press ALT F11 to be taken to the vba screen. On the left hand side you should see Sheet1, Sheet2,Sheet3 and
"This Workbook"

Double click on it and over on the right, from the drop down boxes, select "Workbook" and then "Sheet Calculate"

The macro will then work for EVERY sheet in the workbook, so be careful

You can write some code so that it only acts on the sheets you require, i.e If activesheet.name ="Not This One"
then exit sub

and after that put your code in

Nigel.
User avatar
to75ne
Posts: 2413
Joined: Wed Apr 22, 2009 5:37 pm

sorry to resurrect yet again

i use the following simple macro

Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Range("O6").Select
Selection.ClearContents
Range("K5").Select
End Sub

all it does if o6 the global status cell states "ok",
(becuse the command in the global command field L6 as fired). i can press control and "q". and clear cell o6 (reset the sheet) and return the cursor to cell k5 for convenience.

i would like this to happen automatically every time the command in L6 is fired but, i cant get it to happen no matter how many times i alter the previous code examples on this thread i,e, the calculate event example.

i dont wish to change the status of any other cell or cells, just 06.

obviously im doing something wrong?

could somebody point me in the right direction please.
nigelk
Posts: 469
Joined: Wed Apr 15, 2009 11:00 pm

I hardly ever bother with it now.

Can anyone help?
User avatar
ODPaul82
Posts: 683
Joined: Sun May 08, 2011 6:32 am
Location: Digswell Herts

Try using these events

Private Sub Worksheet_Change(ByVal Target As Range)
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub


Regards
Paul.
Post Reply

Return to “Tips and tricks”