Thread: MacroHelp
View Single Post
  #5   Report Post  
JE McGimpsey
 
Posts: n/a
Default

Better:

Public Sub ConstantsToFormulae()
Const sTEMPLATE As String = "=$$+0"
Dim rTargets As Range
Dim rCell As Range
With Selection
If .Count = 1 Then
If IsNumeric(.Value) Then _
Set rTargets = .Cells
Else
On Error Resume Next 'in case no selected constants
Set rTargets = .SpecialCells( _
xlCellTypeConstants, xlNumbers)
On Error GoTo 0
End If
End With
If Not rTargets Is Nothing Then
For Each rCell In rTargets
With rCell
.Formula = Replace(sTEMPLATE, "$$", _
Int(.Value * 1000) / 1000)
End With
Next rCell
End If
End Sub


Since SpecialCells returns all cells in the Used Range if only one cell
is selected, this modification will prevent overwriting all constants in
that case.

Also, since XL will parse the formula and ignore the format if there are
fewer than three significant figures after the decimal point, there's no
reason to use Format()

In article ,
JE McGimpsey wrote:

One way: