Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Changing cell formulas with a macro or VBA

Have a co-worker that wants to change the formulas in his spreadsheet
to add "=round(" to the front and ",0" to the end. Formulas may be
long or short and there are plenty of them.

He would like a simple macro so he could activate a cell, and hit the
shortcut keys, and make the conversion.

Thanks for any help.
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Changing cell formulas with a macro or VBA

JE McGimpsey just posted this in response to a similar post:

One way:

Select the range of cells you want to convert. The range can include
Text, constants, etc - only the formulae will be converted. Change
the "0" in the cell.Formula line to the number of places you want to
round to.

Public Sub WrapARound()
Dim cell As Range
On Error Resume Next
For Each cell In Selection.SpecialCells(xlCellTypeFormulas)
cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)"
Next cell
On Error GoTo 0
End Sub


You might add: xlNumbers in the Special Cells and also, if he is only
going to do one cell, then I added some code for that as well

Public Sub Wrap_A_Round()
Dim cell As Range, rng as Range
On Error Resume Next
if Selection.Count = 1 then
if selection.HasFormula then
set rng = cell
else
set rng = Nothing
end if
else
set rng = Selection.SpecialCells(xlCellTypeFormulas,xlNumber s)
End if
if not rng is nothing then
For Each cell In rng
cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)"
Next cell
End if
On Error GoTo 0
End Sub

--
Regards,
Tom Ogilvy

"JDeBeer" wrote in message
om...
Have a co-worker that wants to change the formulas in his spreadsheet
to add "=round(" to the front and ",0" to the end. Formulas may be
long or short and there are plenty of them.

He would like a simple macro so he could activate a cell, and hit the
shortcut keys, and make the conversion.

Thanks for any help.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Changing cell formulas with a macro or VBA

or to use David McRitchie's technique (makes the code more compact and fewer
instructions)

Public Sub Wrap_A_Round()
Dim cell As Range, rng as Range
On Error Resume Next
set rng =
Intersect(Selection.Selection.SpecialCells(xlCellT ypeFormulas,xlNumbers))
if not rng is nothing then
For Each cell In rng
cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)"
Next cell
End if
On Error GoTo 0
End Sub


--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
JE McGimpsey just posted this in response to a similar post:

One way:

Select the range of cells you want to convert. The range can include
Text, constants, etc - only the formulae will be converted. Change
the "0" in the cell.Formula line to the number of places you want to
round to.

Public Sub WrapARound()
Dim cell As Range
On Error Resume Next
For Each cell In Selection.SpecialCells(xlCellTypeFormulas)
cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)"
Next cell
On Error GoTo 0
End Sub


You might add: xlNumbers in the Special Cells and also, if he is only
going to do one cell, then I added some code for that as well

Public Sub Wrap_A_Round()
Dim cell As Range, rng as Range
On Error Resume Next
if Selection.Count = 1 then
if selection.HasFormula then
set rng = cell
else
set rng = Nothing
end if
else
set rng =

Selection.SpecialCells(xlCellTypeFormulas,xlNumber s)
End if
if not rng is nothing then
For Each cell In rng
cell.Formula = "=ROUND(" & Mid(cell.Formula, 2) & ",0)"
Next cell
End if
On Error GoTo 0
End Sub

--
Regards,
Tom Ogilvy

"JDeBeer" wrote in message
om...
Have a co-worker that wants to change the formulas in his spreadsheet
to add "=round(" to the front and ",0" to the end. Formulas may be
long or short and there are plenty of them.

He would like a simple macro so he could activate a cell, and hit the
shortcut keys, and make the conversion.

Thanks for any help.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Changing cell formulas with a macro or VBA


If ActiveCell.Formula < "" Then
ActiveCell.Formula = "=ROUND(" _
& Right(ActiveCell.Formula, Len(ActiveCell.Formula) _
- 1) & ",0)"
End If

If I undestood right:

1. check cell has formula
2. attach =round( and ,0) to the cell's existing formula
minus original '=' (always 1st) char.


-----Original Message-----
Have a co-worker that wants to change the formulas in his

spreadsheet
to add "=round(" to the front and ",0" to the end.

Formulas may be
long or short and there are plenty of them.

He would like a simple macro so he could activate a cell,

and hit the
shortcut keys, and make the conversion.

Thanks for any help.
.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 576
Default Changing cell formulas with a macro or VBA

This worked in Excel97

Sub myformula()
' Keyboard Shortcut: Ctrl+q

ActiveCell.Formula = "=round(" & Right(ActiveCell.Formula,
Len(ActiveCell.Formula) - 1) & ",0)"
End Sub

watch word wrap
change 'q' to your choice.

--
sb
"JDeBeer" wrote in message
om...
Have a co-worker that wants to change the formulas in his spreadsheet
to add "=round(" to the front and ",0" to the end. Formulas may be
long or short and there are plenty of them.

He would like a simple macro so he could activate a cell, and hit the
shortcut keys, and make the conversion.

Thanks for any help.



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Changing cell formulas with a macro or VBA

Thanks for everyone's help.

jdb
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
changing cell formulas JulianP Excel Discussion (Misc queries) 1 July 23rd 09 01:10 PM
copying formulas and changing cell references mainsol Excel Discussion (Misc queries) 3 February 9th 09 09:42 AM
changing cell background colors with formulas postal vet Excel Worksheet Functions 5 October 30th 08 07:07 AM
Changing Multiple Cell Formulas zephyr Excel Discussion (Misc queries) 3 May 14th 07 06:35 PM
Changing Cell References in Formulas Pat Excel Worksheet Functions 2 December 15th 04 05:29 PM


All times are GMT +1. The time now is 10:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"