code for checking if first character of existing formula is "="

mdd16

Board Regular
Joined
Jan 11, 2011
Messages
84
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have written a code whereby I am converting the formula of selected cells to round formula without having to type the entire text. I am adding a Prefix String and a Suffix string to the existing formula of the cell. I have copied the code at the end of this message.

I had assumed that all cells to round off will have formula that calculates and therefore will have a "=" to begin with and for that I was neglecting first character and retaining the rest of existing string to be accommodated between prefix and suffix string. I have also added radio buttons a my user form to make the output round off to whole number multiple of either 5 or 10.

Problem : I need to apply " if the first character of existing string is "=" then apply Mid(SelR.Formula, 2), Else if first character is not "=" use full text in the new formula with rounding off text.

I hope I am able to explain what I want.. please help me.

Thanks




VBA Code:
Sub AddStr_Round()
Dim SelR As Range


If Fm_AddString.OpBn_5.value = True Then
    X = 5
ElseIf Fm_AddString.OpBn_10.value = True Then
    X = 10
End If


If Fm_AddString.OpBn_Up.value = True Then

    If Fm_AddString.OpBn_5.value = True Or Fm_AddString.OpBn_10.value = True Then
        Prefix = "Roundup(("
        suffix = ")/" & X & ",0)*" & X
     Else
        Prefix = "Roundup("
        suffix = ",0)"
      End If
ElseIf Fm_AddString.OpBn_Dn.value = True Then
    If Fm_AddString.OpBn_5.value = True Or Fm_AddString.OpBn_10.value = True Then
        Prefix = "RoundDown(("
        suffix = ")/" & X & ",0)*" & X
    Else
        Prefix = "RoundDown("
        suffix = ",0)"
    End If
Else
        Prefix = "Round(("
        Decim = InputBox("Enter Rounding To digits", "Round Formula", 0)
        suffix = ")," & Decim & ")"
End If

For Each SelR In Selection
      SelR.Formula = "=" & Prefix & Mid(SelR.Formula, 2) & suffix
 Next
 Fm_AddString.Hide
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Some further explanation here..

with my current code,
if the original contents of cell is "=123.45". Then I am getting new formula as =ROUND((123.45),0). This is correct.
But if original contents of cell is without "=" ie. "123.45". Then I am getting new formula as =ROUND((23.45),0). This is not what I want.

I need a code to add to ensure that
If there is no "=" in original text then the entire string "123.45" should be used in the new formula. If there is "=" as the first character then the balance text after "=" should appear sandwiched between the prefix and suffix
 
Upvote 0
VBA Code:
If SelR.HasFormula then...

Since if the first character isn't an = sign, it's not really a formula.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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