ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   adding a formula around a cell reference (https://www.excelbanter.com/excel-discussion-misc-queries/143111-adding-formula-around-cell-reference.html)

Greek77

adding a formula around a cell reference
 
I am trying to add a formula in a series of cells that already have a cell
reference.
Example:
(current) =B4
(change to) =round(B4,3)

I have a long series of data that I want to preform a round function on but
do not want to have to do this one by one since there are so many.
Is it possible to add this formula without having to do this procedure one
cell at a time?

dlw

adding a formula around a cell reference
 
you could use find and replace, but you would still have to enter each cell
reference

"Greek77" wrote:

I am trying to add a formula in a series of cells that already have a cell
reference.
Example:
(current) =B4
(change to) =round(B4,3)

I have a long series of data that I want to preform a round function on but
do not want to have to do this one by one since there are so many.
Is it possible to add this formula without having to do this procedure one
cell at a time?


Gary''s Student

adding a formula around a cell reference
 
Try this simple macro:

Sub transformer()
' more than meets the eye
For Each r In Selection
s1 = r.Formula
s2 = Right(s1, Len(s1) - 1)
r.Formula = "=ROUND(" & s2 & ",3)"
Next
End Sub

Enter the macro, select the cells, run the macro.
--
Gary''s Student - gsnu200722

JE McGimpsey

adding a formula around a cell reference
 
One way:

Public Sub WrapARound()
Const sWRAPPER As String = "=Round(#, 3)"
Dim rFormulae As Range
Dim rCell As Range
On Error Resume Next 'in case no formulae
Set rFormulae = Selection.Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If Not rFormulae Is Nothing Then
For Each rCell In rFormulae
With rCell
If Not .Formula Like "=ROUND(*" Then _
.Formula = Application.Substitute( _
sWRAPPER, "#", Mid(.Formula, 2))
End With
Next rCell
End If
End Sub


In article ,
Greek77 wrote:

I am trying to add a formula in a series of cells that already have a cell
reference.
Example:
(current) =B4
(change to) =round(B4,3)

I have a long series of data that I want to preform a round function on but
do not want to have to do this one by one since there are so many.
Is it possible to add this formula without having to do this procedure one
cell at a time?


Greek77

adding a formula around a cell reference
 
GREAT! This simple macro saved me hours of tedious work!!
Thanks

"Gary''s Student" wrote:

Try this simple macro:

Sub transformer()
' more than meets the eye
For Each r In Selection
s1 = r.Formula
s2 = Right(s1, Len(s1) - 1)
r.Formula = "=ROUND(" & s2 & ",3)"
Next
End Sub

Enter the macro, select the cells, run the macro.
--
Gary''s Student - gsnu200722



All times are GMT +1. The time now is 07:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com