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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Can be done. In which cell area are the formulas placed? E.g. Cells A1 to K12.
 
Upvote 0
May be:
PHP:
Sub test()
For Each Rng In Range("b2:e100")
If Rng.HasFormula = True And Rng.Value > 0 Then
Rng.Value = Rng.Value
End If
Next
End Sub
 
Upvote 0
VBA Code:
Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("b2:e100").Cells
  If Rng.HasFormula = True And Rng.Value > 0 Then
    Rng = "'" & Format(Rng)
  End If
Next Rng
End Sub
 
Last edited:
Upvote 0
Forgot to check for #REF Errors in above code...:

VBA Code:
Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("b2:e100").Cells
  If Rng.HasFormula = True And Rng.Value > 0 And IsError(Rng)=False Then
    Rng = "'" & Format(Rng)
  End If
Next Rng
End Sub[CODE]
 
Upvote 0
Hi

Thank you for this - when I run the Macro it comes up as "Type Mismatch"?

Forgot to check for #REF Errors in above code...:

VBA Code:
Sub ChgF()
Dim Rng As Range
For Each Rng In ActiveSheet.Range("b2:e100").Cells
  If Rng.HasFormula = True And Rng.Value > 0 And IsError(Rng)=False Then
    Rng = "'" & Format(Rng)
  End If
Next Rng
End Sub[CODE]
 
Upvote 0
Hm... works for me. Try this:

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Rng As Range
For Each Rng In Range("B2:E100").Cells
  If Rng.HasFormula = True And IsNumeric(Rng)=True And Rng.Value > 0 And IsError(Rng) = False Then
    Rng = "'" & Format(Rng)
  End If
Next Rng
End Sub
 
Upvote 0
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
As hard as I try, I can't reproduce any Type Mismatch error with this code...

Hi, what if you try with this formula in cell B2
Excel Formula:
=INDEX(A1,2)
 
Upvote 0

Forum statistics

Threads
1,216,163
Messages
6,129,223
Members
449,495
Latest member
janzablox

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