VBA that converts a formula to text/number if result is more than 0 / (or NOT #REF!)

surkdidat

Well-known Member
Joined
Oct 1, 2011
Messages
582
Office Version
  1. 365
Is there anyway I can have VBA code run automatically/when a work book closes/click of a button etc, that goes through a sheet that contains a formula.

If the formula "answer" is a number more than 0 (or is simply not a #REF!) I want it then to convert the umbers permanently to text
 
Hi, what if you try with this formula in cell B2
Excel Formula:
=INDEX(A1,2)
:) ...that produces a Mismatch. Great.

Change to:

VBA Code:
Public Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("B2:E100").Cells
  If IsError(Rng) = False Then
    If Rng.HasFormula = True And IsNumeric(Rng) = True And Rng > 0 Then
      Rng = "'" & Format(Rng)
    End If
  End If
Next Rng
End Sub
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Could it be if B2 is in #REF! status, ie the first value does not have a numeric value? Is it easy to amend so I can add "IFERROR" with either a blank or a " - "?

Thanks, and apologies for all the queries on this!
As hard as I try, I can't reproduce any Type Mismatch error with this code...

VBA Code:
Public Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("B2:E100").Cells
  If Rng.HasFormula = True And IsNumeric(Rng) = True And Rng > 0 And IsError(Rng) = False Then
    Rng = "'" & Format(Rng)
  End If
Next Rng
End Sub
 
Upvote 0
This (as posted above) will work:

VBA Code:
Public Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("B2:E100").Cells
  If IsError(Rng) = False Then
    If Rng.HasFormula = True And IsNumeric(Rng) = True And Rng > 0 Then
      Rng = "'" & Format(Rng)
    End If
  End If
Next Rng
End Sub

If you cut out the code betwen Sub ChgF() and End Sub and place it in the Workbook in the Workbook_BeforeClose sub, the macro will execute automatically every time you close the workbook.

Just because I'm curious: why is it, that you want the formulas replaced by numeric values formatted as text?
 
Upvote 0
This (as posted above) will work:

VBA Code:
Public Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("B2:E100").Cells
  If IsError(Rng) = False Then
    If Rng.HasFormula = True And IsNumeric(Rng) = True And Rng > 0 Then
      Rng = "'" & Format(Rng)
    End If
  End If
Next Rng
End Sub

If you cut out the code betwen Sub ChgF() and End Sub and place it in the Workbook in the Workbook_BeforeClose sub, the macro will execute automatically every time you close the workbook.

Just because I'm curious: why is it, that you want the formulas replaced by numeric values formatted as text?
NUmeric/Text, apologies probably lost in what I was saying. basically, was trying to distinguish the end result from a formula :)
 
Upvote 0
NUmeric/Text, apologies probably lost in what I was saying. basically, was trying to distinguish the end result from a formula :)
If distinguishing in a sense of "seeing" is meant, wouldn't it be sufficient to mark the cells containing a formula by, let's say changing the background collor?
 
Upvote 0
If distinguishing in a sense of "seeing" is meant, wouldn't it be sufficient to mark the cells containing a formula by, let's say changing the background collor?
Need it to be converted form a formula, as other spreadsheets link into this, so the issue I was having is when the were closed, it "lost" the data. Having something that has all the formulas in, waiting to "capture" the data when its fed in, and then converting it to a number/text in the cell gets around the problem :)
 
Upvote 0
Need it to be converted form a formula, as other spreadsheets link into this, so the issue I was having is when the were closed, it "lost" the data. Having something that has all the formulas in, waiting to "capture" the data when its fed in, and then converting it to a number/text in the cell gets around the problem :)
Okay, I see.
Please note that my vba-macro I provided converts all numbers to text. So if a numerical value is to be read out, the text needs to be reconverted to a number.
You can change this behaviour by changing
VBA Code:
Rng = "'" & Format(Rng)
in the code to
VBA Code:
Rng = Rng
Then all texts will remain text, all numbers will remain numbers.
I understood your initial post that you wanted them converted to text.
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,340
Members
449,311
Latest member
accessbob

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