Excel VBA: Code to add ' in front of the = sign in front of the formula in a selected range of cells.

How would I write a code that would add ' in front of the = sign in the beginning of all formulas in the active selected range of cells, converting formulas to text?
Dmitriy KritskiyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can do something like this to do what you are looking for..

Sub changeformulas()
Dim rng As Range, cell As Range

Set rng = Selection
 ActiveWindow.DisplayFormulas = True
For Each cell In rng

If InStr(1, cell.Text, "=", vbTextCompare) > 0 Then cell.Value = "'" & cell.Text
Next cell

ActiveWindow.DisplayFormulas = False
End Sub

Open in new window


Again this will change formulas to text of that range only which you have selected not for entire sheet..

Saurabh....
0
Anthony BerenguelCommented:
use this
Sub Convert_Formulas_To_Text()
    Dim rng As Range
    Dim c As Range
    Dim t As String
    
    Set rng = Selection
    
    For Each c In rng
        t = c.Formula
        If Left(t, 1) = "=" Then
            c.Value = "'" & t
        End If
    Next c
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roy CoxGroup Finance ManagerCommented:
You can change settings to show formulas in cells using Excel Options

Code could be

On Error Resume Next
Selection.SpecialCells(xlCellTypeFormulas).Value = "'" & Selection.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
A slight amendment to my code will be much faster than a Loop.

On Error Resume Next
Selection.SpecialCells(xlCellTypeFormulas).Value = "'" & Selection.SpecialCells(xlCellTypeFormulas).Formula
On Error GoTo 0

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.