Is it possible to set the numberformat in a cell/column using either Excel or VBA so that:
I'm having a problem where I want all user input to be stored as text, but users should also be able to enter formulas. If I set the numberformat to text, formulas aren't interpreted. If I set the numberformat to general, values are stored as numbers.
Here is my version.
Format all cells in that sheet as Text
. This code uses Application.Evaluate()
to evaluate all formulas and store the result as text.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aCell As Range
On Error GoTo Whoa
Application.EnableEvents = False
'~~> You need this in case user copies formula
'~~> from another sheet
Target.Cells.NumberFormat = "@"
'~~> Looping though all the cells in case user
'~~> copies formula from another sheet
For Each aCell In Target.Cells
If Left(aCell.Formula, 1) = "=" Then _
aCell.Value = Application.Evaluate(aCell.Formula)
Next
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With