Alfredhigh
New Member
- Joined
- Aug 11, 2022
- Messages
- 10
- Office Version
- 2010
- Platform
- Windows
What is the VBA code to convert "M" to six zero and "B" to nine zero?That is controlled by the format you have on that cell.
Just change it off the "Scientific" format to "General" or "Number", and it will appear the way you want.
View attachment 71922
How to inserted this VBA code?The custom number format [<999999]0.00;[<99999900]0.00,," M";0.00,,," B" will display numbers in the format shown. (This uses a US billion)
Click where to do that?That is a custom number format, not VBA code. If you format your cells with that format. It will take the numbers entered (as numbers) and display them in the format shown. Then the normal operations, like sorting, will act as expected.
The OP shows a sheet with text entries, which is the root of the problem. Changing the sheet values to numbers will correct those problems.
Sub ConvertNums()
Dim cell As Range
Application.ScreenUpdating = False
' Change format of selected cells
Selection.NumberFormat = "0"
' Run against every cell in selected range
For Each cell In Selection
Select Case Right(cell, 1)
Case "M"
cell.Value = Left(cell, Len(cell) - 1) * 1000000
Case "B"
cell.Value = Left(cell, Len(cell) - 1) * 1000000000
End Select
Next cell
Application.ScreenUpdating = True
End Sub
I can't find this [<999999]0.00;[<99999900]0.00,," M";0.00,,," B" in the format cell dialog box.In the Format Cell dialog box.
Hi Joe4, May I know add what line to change the cell format?I already provided you with the code last week up in post #6 in order to change the values to valid numbers.
You can simply add a line to change the cell format at the same time, i.e.
VBA Code:Sub ConvertNums() Dim cell As Range Application.ScreenUpdating = False ' Change format of selected cells Selection.NumberFormat = "0" ' Run against every cell in selected range For Each cell In Selection Select Case Right(cell, 1) Case "M" cell.Value = Left(cell, Len(cell) - 1) * 1000000 Case "B" cell.Value = Left(cell, Len(cell) - 1) * 1000000000 End Select Next cell Application.ScreenUpdating = True End Sub
If you look closely, and compare the code I posted in my last post to the code I posted previously, you will see that I already added it for you:Hi Joe4, May I know add what line to change the cell format?
Selection.NumberFormat = "0"