2 questions

alokjain22

New Member
Joined
Sep 15, 2006
Messages
1
I am good in excel. However i do not know how to solve the following two problems:-

1. Say i have a formula in Column C = "ColumnA+ColumnB (a1+b1, a2+b2, ... like that in all rows of column C. Now i want to add an 'IF' condition in the same row. (e.g.=if(a1+b1>100,100,a1+b1). One way of doing is entering this formula and copy it in all rows of col C. But what if Column C have different formulas, and i have to add the 'IF' condition (or any ther thing) in all rows of col C irrespective of difference of formulas.

2. In a Macro, how to choose the End of the sheet, when it is not a fixed cell?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Try this:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim Cell As Range
    Set Sh = Worksheets("Sheet1")
    With Sh
        Set Rng = .Range("C1:C" & .Range("C" & .Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeFormulas)
    End With
    For Each Cell In Rng
        With Cell
            .FormulaR1C1 = "=IF((RC[-2]+RC[-1])>100,100," & Replace(.FormulaR1C1, "=", "") & ")"
        End With
    Next Cell
End Sub

In the sixth line, this:

.Range("C" & .Rows.Count).End(xlUp)

returns the last used cell in column C.
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi Andrew

I was thinking that instead of

Replace(.FormulaR1C1, "=", "")

maybe

Mid(.FormulaR1C1, 2)

in case the formula has other "=" besides the first.

Cheers
PGC
 

Forum statistics

Threads
1,136,366
Messages
5,675,355
Members
419,565
Latest member
Phil57

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top