[VBA] Edit formula

darkfreedoom

New Member
Joined
Nov 1, 2011
Messages
30
Dear Mr/Ms,

I would like to create a VBA to edit my excel cells' formula by adding some info to the starting & ending of the formula.

I created this but it doesn't work:

Rich (BB code):
Dim Srow, Erow, Lrow, Scol, Ecol, Lcol
Srow = Selection.Row
Erow = Srow + Selection.Rows.Count - 1
Scol = Selection.Column
Ecol = Scol + Selection.Columns.Count - 1


For Lrow = Srow To Erow Step 1
    Dim i
    i = Cells(Lrow, Scol).FormulaR1C1
    If IsEmpty(i) Then
        GoTo CSub
    Else
        Cells(Lrow, Scol).FormulaR1C1 = "=Round(" & i & ",2)"
    End If
CSub:
Next Lrow

if I change i = Cells(Lrow, Scol) ,
it run, but it paste the value of the cell.
i want it to paste the formula back.
Any idea?

Thanks in advance.

Regards
Vincent
WinXP / MSExcel07
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi friend,

use below code--->

************************************************************
Sub MyCode()

Dim Srow, Erow, Lrow, Scol, Ecol, Lcol
Srow = Selection.Row
Erow = Srow + Selection.Rows.Count - 1
Scol = Selection.Column
Ecol = Scol + Selection.Columns.Count - 1

For Lrow = Srow To Erow Step 1
Dim i As String
i = Replace(Cells(Lrow, Scol).Formula, "=", "")
If IsEmpty(i) Then
GoTo CSub
Else
Cells(Lrow, Scol).Formula = "=Round(" & i & ",2)"
End If
CSub:
Next Lrow

End Sub

************************************************************

Yogesh Dalimbkar
 
Upvote 0
Hi friend,

use below code--->

************************************************************
Sub MyCode()

Dim Srow, Erow, Lrow, Scol, Ecol, Lcol
Srow = Selection.Row
Erow = Srow + Selection.Rows.Count - 1
Scol = Selection.Column
Ecol = Scol + Selection.Columns.Count - 1

For Lrow = Srow To Erow Step 1
Dim i As String
i = Replace(Cells(Lrow, Scol).Formula, "=", "")
If IsEmpty(i) Then
GoTo CSub
Else
Cells(Lrow, Scol).Formula = "=Round(" & i & ",2)"
End If
CSub:
Next Lrow

End Sub

************************************************************

Yogesh Dalimbkar

thanks.
i see my mistakes.
so i have to eliminate the "=" sign before adding the info.
:)

thanks, LIKE given

///

but if i apply with your codes, when i selected blank cells, they won't be blank anymore :/
 
Last edited:
Upvote 0
Thanks for your hints, and here's the code i rebuilt that work fine :)

Code:
Dim Srow, Erow, Lrow, Scol, Ecol, Lcol
Srow = Selection.Row
Erow = Srow + Selection.Rows.Count - 1
Scol = Selection.Column
Ecol = Scol + Selection.Columns.Count - 1


For Lrow = Srow To Erow Step 1
    Dim i
    i = Replace(Cells(Lrow, Scol).FormulaR1C1, "=", "")
    If i = "" Then
        GoTo CSub
    Else
        Cells(Lrow, Scol).FormulaR1C1 = "=Round(" & i & ",2)"
    End If
CSub:
Next Lrow
 
Upvote 0

Forum statistics

Threads
1,216,366
Messages
6,130,194
Members
449,565
Latest member
excelqueenintraining

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
Back
Top