Hi there,
I have a simple formula procedure which is linked to a button. When the button is clicked, nothing happens even though ranges have values.
Columns have values, but this button doesn't really clear them.Dim cell As Object For Each cell In Worksheets("test").Range("F2:J40") If cell.Value <> 0 Then cell.ClearContents Next cell End Sub
Is anyone able to assist?
Thanks
Last edited by Lifeseeker; 01-07-2012 at 01:33 AM.
Instead try:
I usually avoid using "CELL" as a variable name. I also re-dimmed ce as a Range instead of an Object.Dim ce As Range For Each ce In Worksheets("test").Range("F2:J40") If ce.Value <> 0 Then ce.ClearContents Next ce End Sub
Hi Paul,
nothing happens...I wonder if it has to do with how values on the range F2:J40 are entered. They are populated automatically by clicking buttons. (so they are not manually entered into the spreadsheet). But this doesn't mean it is the reason for why code doesn't work?Private Sub ClearALL_Click() Dim ce As Range For Each ce In Worksheets("test").Range("F2:J40") If ce.Value <> 0 Then ce.ClearContents Next ce End Sub
Just as a test, if you run
do you see the message?Private Sub ClearALL_Click() Dim ce As Range msgbox "code is running" For Each ce In Worksheets("test").Range("F2:J40") If ce.Value <> 0 Then ce.ClearContents Next ce End Sub
Good luck.
OnError's suggestion was going to be next... are your macros enabled? Or did they somehow get disabled (via code or not enabling them when opening the workbook)?
Please see the attached....this is pretty frustrating as there is no problem with the code, but it's not running...(the message box is running, but no cell contents are cleared...)
Thanks
Button is to the right hand side
try
Private Sub ClearALL_Click() Dim ce As Range With Worksheets("New CCM Reporting Clinic-Centre").Range("F2:J40") .Value = .Range("F2:J40").Value For Each ce In .Range("F2:J40") If ce.Value <> 0 Then ce.ClearContents Next ce End With End Sub
regards pike
If the solution helped please donate here to the RSPCA
Sites worth visiting;
J&R Solutions - royUK
AJP Excel Information - Andy Pope
Spreadsheet Toolbox
VBA for smarties - snb
I downloaded your workbook, and noticed that the only values in F2:J40 were zeroes. (0, 0%, etc.). This macro won't remove any of those since it's explicitly checking for only cells that are not equal to 0 and clearing those. If you want it to clear cells that *are* 0, change "<>0" to "=0".
I entered a few non-zero values into that range, clicked the button and it worked just fine - leaving blank cells wherever those values were entered.
ah I see.
So if I wanted to remove both 0 and 0% AND non zeros( so as long as a cell has something in it), I would use an OR statement or something like that?
By the way, in practice, I only want to apply this on the F column to clear the formula from formula bar. (The F2:J40 is just small test).
This F column will contains both 0 and actual numbers. I want to have a macro that removes all of them.
I had thought that a simple like this would do the trick, but guess not.
Only removed 0 but not other numbers from column F. In practice, I want this to be done only on col F, which may contain BOTH 0 and other numbers.
If you simply want to clear the formulas/contents of all cells in column F (from F2 downward), just use
Dim lastRowF as Long lastRowF = Range("F" & Rows.Count).End(xlUp).Row Range("F2:F" & lastRowF).ClearContents
ahh I see I see.....
Thank you Paul...I've learned so much from you just today....
I still have lots to learn even in Excel...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks