PDA

View Full Version : Solved: Zero the negatives



mikedbom
06-30-2011, 10:28 AM
i've been trying to modify this code for my worksheet but keep getting a type mismatch error at the worksheets("sheet10") declaration. I'm trying get my sheet to resolve all negative numbers to 0. Also, i dont want this code running the whole time, on command would be fine. Any help would be greatly appreciated.

Here is the code:
Sub ZeroNegatives()
Dim sheet As Worksheet
Dim cl As Range
For Each sheet In Worksheets("Sheet10")
For Each cl In sheet.Range("B4:F34")

If cl.Value < 0 Then
cl.Value = 0
Exit For
End If
Next cl
Next sheet
End
End Sub

mikerickson
06-30-2011, 11:24 AM
Worksheets("Sheet10") is a single worksheet. If you want to loop, you need a collection. You might use
For Each sheet in Worksheets(Array("Sheet10"))

Or, if you don't want to loop, you could use

Sub ZeroNegatives()
Dim cl As Range

With Worksheets("Sheet10")
For Each cl In .Range("B4:F34")
If cl.Value < 0 Then
cl.Value = 0
End If
Next cl
End With
End Sub

Aussiebear
07-25-2011, 01:02 AM
It would seem that your answer is more than sufficient Mike, so I'm marking this thread as solved