Return formula minus = sign
Hi Dave.
I am very pleased for your advise, and I do hope, I do you credit with
below.
Thanks.
Sub test()
Dim MyString As String
MyString = OnlyString(ActiveCell)
Stop
End Sub
'----------------------------------------------------------
' Procedure : OnlyString
' Date : 20060904
' Author : Joergen Bondesen
' Modifyed by :
' Purpose : Display Formula without leading '=' and
' leading '+'
' Note : Eg. =5+2 = '5+2' or =+B5+C3 = 'B5+C3'
' Function select always cell(1).
'----------------------------------------------------------
'
Function OnlyString(Rng As Range) As String
OnlyString = ""
With Rng.Cells(1)
If .HasFormula Then
If Mid(.Formula, 2, 1) = "+" Then
OnlyString = _
Mid(.Formula, 3, Len(.Formula) - 2)
Else
OnlyString = _
Mid(.Formula, 2, Len(.Formula) - 1)
End If
End If
End With
End Function
--
Best regards
Joergen Bondesen
"Dave Peterson" wrote in message
...
I'm not sure I'd check anything but .hasformula (drop the typename() and
isempty() checks???)
This is what I use to return the formula (including the equal sign) (just
for
comparison purposes):
Option Explicit
Function GetFormula(Rng As Range)
Dim myFormula As String
GetFormula = ""
With Rng.Cells(1)
If .HasFormula Then
If Application.ReferenceStyle = xlA1 Then
myFormula = .Formula
Else
myFormula = .FormulaR1C1
End If
If .HasArray Then
GetFormula = "{=" & Mid(myFormula, 2, Len(myFormula)) & "}"
Else
GetFormula = myFormula
End If
End If
End With
End Function
Joergen Bondesen wrote:
Hi Dave.
Thanks. You are quite right.
My brain must have been disabeled for a few seconds.
Notice the way I am using mid with 2 parameters. 8-)
Hope you can approve belove.
Option Explicit
Sub test()
Dim MyString As String
MyString = OnlyString(ActiveCell)
Stop
End Sub
'----------------------------------------------------------
' Procedure : OnlyString
' Date : 20060903a
' Author : Joergen Bondesen
' Modifyed by :
' Purpose : Display Formula without leading '=' and
' leading '+'
' Note : Eg. =5+2 = '5+2' or =+B5+C3 = 'B5+C3'
'----------------------------------------------------------
'
Function OnlyString(cell As Range) As String
If TypeName(cell.Value) = "String" Or _
IsEmpty(cell.Value) Then
OnlyString = "No formula."
Exit Function
End If
If cell.HasFormula Then
If Mid(cell.Formula, 2, 1) = "+" Then
OnlyString = _
Mid(cell.Formula, 3, Len(cell.Formula) - 2)
Else
OnlyString = _
Mid(cell.Formula, 2, Len(cell.Formula) - 1)
End If
End If
End Function
--
Best regards
Joergen Bondesen
"Dave Peterson" wrote in message
...
I can understand stripping the + sign from after the initial = sign, but
I
don't
think removing =- would be a good idea. That negative sign looks
important to
the formula.
=-a1+b1
would return
a1+b1
And that doesn't look like it reflects what the formula really does.
Joergen Bondesen wrote:
Hi Patrick
Try below please.
Option Explicit
Sub test()
Dim MyString As String
MyString = OnlyString(Range("D8"))
End Sub
'----------------------------------------------------------
' Procedure : OnlyString
' Date : 20060903
' Author : Joergen Bondesen
' Modifyed by :
' Purpose : Display Formula without leading '=' and
' leading '+/-'
' Note : Eg. =5+2 = '5+2' or =+B5+C3 = 'B5+C3'
'----------------------------------------------------------
'
Function OnlyString(cell As Range) As String
If TypeName(cell.Value) = "String" Or _
IsEmpty(cell.Value) Then
OnlyString = "No formula."
Exit Function
End If
If cell.HasFormula Then
If Mid(cell.Formula, 2, 1) = "+" Or _
Mid(cell.Formula, 2, 1) = "-" Then
OnlyString = _
Mid(cell.Formula, 3, Len(cell.Formula) - 2)
Else
OnlyString = _
Mid(cell.Formula, 2, Len(cell.Formula) - 1)
End If
End If
End Function
--
Best Regards
Joergen Bondesen
"Patrick Simonds" wrote in message
...
Is there any way to get the code below to return formula in cell B4
without the = sign. So if the cell contains =10+10+4.50 I want
only
10+10+4.50
TextBox93.Text = Worksheets("Income").Range("B4").Text
--
Dave Peterson
--
Dave Peterson
|