Display Number as Text using Custom Format

MartinS

Active Member
Joined
Jun 17, 2003
Messages
489
Office Version
  1. 365
Platform
  1. Windows
I have a workbook where, in a summary section, some of the values should be displayed as text using the cell format from the source range.
I have found that using Format doesn't work, i.e.
VBA Code:
Function Udf_GetFormattedValue(rngCell As Range) As String
    Dim strResult As String 
    strResult = rngCell.Value
    If IsEmpty(rngCell) Then
        strResult = ""
    Else        
        If StrComp(rngCell.NumberFormat, "General", vbTextCompare) <> 0 Then
            strResult = WorksheetFunction.Text(strResult, rngCell.NumberFormat)
        End If
    End If
    Udf_GetFormattedValue = strResult
End Function
So Udf_GetFormattedValue(7944808.4,"#,##0.0,;(#,##0.0,)") returns 7,944,808.4, when I'd expect 7,944.8.
I have found that swapping Format for WorksheetFunction.Text works, i.e.
VBA Code:
strResult = WorksheetFunction.Text(strResult, rngCell.NumberFormat)
But is there any way without using worksheet functions to return the correctly formatted value from the function?
Many thanks
Martin
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I'm wondering why you think seven million, nine hundred and forty-four thousand, eight hundred and eight point four (7944808.4) could ever be formatted as seven thousand, nine hundred and forty-four point eight. VBA Format function does not change the stored value, it only changes the way it is represented and the two values you posted are not the same. I can't see how the worksheet function Text would do that either. VBA format function does change number data type to string data type though.
 
Upvote 0
The number is being shown in either thousands or millions, as specified by the user. The idea is to display the number in the same format as part of a string.
 
Upvote 0
You can covert numerically per thousands or millions like this...
Book1
AB
1thousands
27,944,808.407,944.80840
Sheet1
Cell Formulas
RangeFormula
B2B2=IF(B1="thousands",A2/1000,A2/1000000)
 
Upvote 0
How about using Range.Text property :
VBA Code:
Range("B1") = Range("A1").Text
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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