Consulting

Results 1 to 5 of 5

Thread: Solved: How to use replace function in VBA?

  1. #1

    Solved: How to use replace function in VBA?

    I want to replace cells from column F to S with zero(0), with a condition IF cells are >0.

    how do you use replace function with that condition?

    I tried doing:
    [VBA]Sub f()
    ActiveSheet.UsedRange.Replace >0, "", xlWhole
    End Sub[/VBA]

    But it just reds out.

    Thanks!

  2. #2
    Could you explain a little ?I can't understand what you want
    Do you want to write a macro that do the same of a formula or what??
    elaborate plz

  3. #3
    VBAX Mentor
    Joined
    Jun 2004
    Posts
    363
    Location
    You can't conditionally replace values using Replace

    The following will replace all numbers greater than Zero in columns F through S

    [vba]Sub test()
    Dim rA As Range, rB As Range
    Set rA = Range("F:S").SpecialCells(xlCellTypeConstants, 1)
    For Each rB In rA
    If rB > 0 Then rB = 0
    Next
    End Sub[/vba]
    Last edited by mbarron; 07-08-2010 at 06:39 PM. Reason: typo

  4. #4
    My condition is:

    If cell is >0 replace it with 0

    I have a previous project where I used:
    [VBA]
    Sub f()
    ActiveSheet.UsedRange.Replace 0, "", xlWhole
    End Sub
    [/VBA]

    It worked, but this is to replace zero with blanks.

    Now, what I want is if the condition 'IF cell is > 0' it will be replaced by "0".

    I tried:
    [VBA]
    Sub f()
    ActiveSheet.UsedRange.Replace > 0, "", xlWhole
    End Sub
    [/VBA]

    But it gives me an error(red highlight)

  5. #5
    Thanks MBarron! This is perfect!

    Mwahhh! Mwahhh! Mwahhh!

Posting Permissions

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