Reputation: 12047
I have a Sub that outputs various data from a Form. Some of that data must be as a currency, formatted as below.
However, although outputting the value in this was does actually set the correct number format for the cell, the contents do not display correctly. Instead, they display as standard text with the warning Number stored as text
.
I do have the option to Convert to number
, but I can't find a way of doing this via VBA (recording a macro while making the change yields no results).
How can I fix this problem? Thanks.
With Selection
.Value = Form.txtMonthlyCost
.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
End With
Upvotes: 0
Views: 449
Reputation: 29328
Since your Data is stored as text you must convert the DataType in the VBA Code
With Selection
.Value = CCur(valueOr(Form.txtMonthlyCost)) 'Convert text to currency Format
.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
End With
.....
Public Function valueOR(val AS Variant, Optional dataType As String = "Number") AS Variant
If isNull(val) then
SELECT CASE dataType
Case "Number"
val = 0
Case "String"
val = ""
End SELECT
End If
valueOR = val
End Function
Also know this will throw an error if a null value is submitted so I added code to handle that as well. I use an expanded version of this to handle Most dataTypes but this should be sufficient for you needs here.
Upvotes: 2