Consulting

Results 1 to 8 of 8

Thread: Solved: How do you write this formula in VBA code?

  1. #1

    Solved: How do you write this formula in VBA code?

    If column C to AX is equal to zero, clear contents.

    Thanks!!!

  2. #2
    Sub test()
    If WorksheetFunction.Sum(Range("C" & ActiveCell.Row & ":AX" & ActiveCell.Row)) = 0 Then
    Range("C" & ActiveCell.Row & ":AX" & ActiveCell.Row).ClearContents
    End If
    End Sub

  3. #3
    It actually run, but zeros were not cleared.

    And, why is there a "sum" function?

    I just actually want to delete the zeros in the cells. I'm not summing up anything.

    Thanks!

  4. #4
    Wasn't to sure what you were asking for, here is the code.

    Sub test2()
    For Each c In Range("C" & ActiveCell.Row & ":AX" & ActiveCell.Row)
    If c.Value = 0 Then
    c.ClearContents
    End If
    Next c
    End Sub

  5. #5
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    A non-looping option would be to use Replace:
    [VBA]Sub NoZero()
    Range("C" & ActiveCell.Row & ":AX" & ActiveCell.Row).Replace _
    what:="0", replacement:="", lookat:=xlWhole
    End Sub[/VBA]

  6. #6
    Thanks!

    It worked!

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I use this to clear selected areas of zero values
    [VBA]
    Sub delzero()
    For Each cel In Intersect(Selection, ActiveSheet.UsedRange)
    If cel.Value = 0 Then cel.ClearContents
    Next
    End Sub

    [/VBA]

    and for Error cells

    [VBA]Sub DelErrors()
    On Error Resume Next
    If MsgBox("Clear error cells?", vbYesNo) = vbYes Then
    Selection.SpecialCells(xlCellTypeFormulas, 16).ClearContents
    Selection.SpecialCells(xlCellTypeConstants, 16).ClearContents
    End If
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    Thanks again mdmakiilop!

Posting Permissions

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