Consulting

Results 1 to 3 of 3

Thread: Solved: Zero the negatives

  1. #1
    VBAX Newbie
    Joined
    Jun 2011
    Posts
    1
    Location

    Solved: Zero the negatives

    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:
    [vba]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[/vba]

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Worksheets("Sheet10") is a single worksheet. If you want to loop, you need a collection. You might use
    [VBA]For Each sheet in Worksheets(Array("Sheet10"))[/VBA]

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

    [VBA]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 [/VBA]

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    It would seem that your answer is more than sufficient Mike, so I'm marking this thread as solved
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •