macro for formatting numbers stored as text

traxdmb34

New Member
Joined
May 2, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm very new to VBA. I'm trying to get numbers stored as text converted to numbers, while keeping the same number of decimals the value had while stored as text and add a comma to any number that is over a thousand. The closest I could get was changing the number format to general, but that doesn't give me commas, and anything with a .0 comes back as a whole number. I also tried a custom format like #,###.##, but that gives me a decimal point on a whole number. Any tips or tricks would be appreciated!

VBA Code:
ws.Range("C4:C" & lastrow, Cells(lngLastColumn)).Select
With Selection
Selection.NumberFormat = "General" 'need to figure how to get floating decimals
.Value = .Value

1683056153319.png
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the Board!

Since your various cells will not all be formatted the same (different number of decimals), you cannot do them all at once like that.

Assuming that your lastrow and lngLastColumn calculations are correct, try replacing the block of code you posted with this:
VBA Code:
    Dim cell As Range
    Dim ln As Long
    Dim dc As Long
    Dim ft As String
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    For Each cell In ws.Range(Cells(4, 3), Cells(lastrow, lngLastColumn))
'       Check to see if cell is numeric
        If IsNumeric(cell) Then
'           Find length of cell
            ln = Len(cell)
'           Find decimal in cell
            dc = InStr(1, cell, ".")
'           Build number format
            If dc > 0 Then
                ft = "#,##0." & Left("0000000000", ln - dc)
            Else
                ft = "#,##0"
            End If
'           Apply cell format
            cell.NumberFormat = ft
'           Enter cell value
            cell.Value = cell.Value
        End If
    Next cell
    
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
 
Upvote 0
Solution
Welcome to the Board!

Since your various cells will not all be formatted the same (different number of decimals), you cannot do them all at once like that.

Assuming that your lastrow and lngLastColumn calculations are correct, try replacing the block of code you posted with this:
VBA Code:
    Dim cell As Range
    Dim ln As Long
    Dim dc As Long
    Dim ft As String
   
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
   
    For Each cell In ws.Range(Cells(4, 3), Cells(lastrow, lngLastColumn))
'       Check to see if cell is numeric
        If IsNumeric(cell) Then
'           Find length of cell
            ln = Len(cell)
'           Find decimal in cell
            dc = InStr(1, cell, ".")
'           Build number format
            If dc > 0 Then
                ft = "#,##0." & Left("0000000000", ln - dc)
            Else
                ft = "#,##0"
            End If
'           Apply cell format
            cell.NumberFormat = ft
'           Enter cell value
            cell.Value = cell.Value
        End If
    Next cell
   
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

thanks for the welcome and THANKS for the help! my issue has been resolved. thanks again!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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