Converting Numbers Stored as Text, into Numbers

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
I have an access query which exports numbers as text. In the past, I would simply insert a column, then multiply the text number by 1, and drag the formula all the way down. Then copy and and paste as values back over the column with text numbers.
My question, is there a VBA code to select the entire column, and change all text numbers into real numbers...all at once?
If I use the recorder, all it tells me is when I select the cell, it doesn't show the step where I convert the error drop-down to number.
Thanks for the help
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
My question, is there a VBA code to select the entire column, and change all text numbers into real numbers...all at once?
You should be able to do this quite easily with Text to Columns.
Just do the following:
- Select your column
- Change the format to a numeric one
- Select "Text to Columns" from the Data menu
- Click "Finish"

If you want this to be VBA code, just turn on the Macro Recorder and record the steps above.
 
Upvote 0
there is another way:

1)select the column contains numbers as text;
2)Ribbon---Data---Text to columns---click Finishi directly.
 
Upvote 0
Code:
there is another way:

1)select the column contains numbers as text;
2)Ribbon---Data---Text to columns---click Finishi directly.

Thank you!
 
Upvote 0
Code:
Columns("E:E").Select
Selection.TextToColumns Destination:=Range("E1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
Selection.NumberFormat = "0"

The code above is working, the text in column E is converted to a number. However, what I really would need is for excel to search in Range (4:4) and find the heading word of "PID", then use the "PID" column to convert the text to numbers, instead of selecting Columns E:E...is this possible?
Thanks for the help
 
Upvote 0
Maybe something like this:
Code:
Sub MyTextToCol()

    Dim fCol As Long
    
    On Error GoTo err_chk
'   Find which column "PID" is in in row 4
    fCol = Rows("4:4").Find(What:="PID", After:=Range("A4"), LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Column
        
'   Perform text to columns on found column
    Columns(fCol).TextToColumns Destination:=Cells(1, fCol), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    
'   Reformat column
    Columns(fCol).NumberFormat = "0"
        
    On Error GoTo 0
    Exit Sub
    
err_chk:
'   Return error message if PID not found
    If Err.Number = 91 Then
        MsgBox "Cannot find PID in row 4!", vbOKOnly, "ERROR!!!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
        
End Sub
 
Upvote 0
Maybe something like this:
Code:
Sub MyTextToCol()

    Dim fCol As Long
    
    On Error GoTo err_chk
'   Find which column "PID" is in in row 4
    fCol = Rows("4:4").Find(What:="PID", After:=Range("A4"), LookIn:=xlFormulas, LookAt:= _
        xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Column
        
'   Perform text to columns on found column
    Columns(fCol).TextToColumns Destination:=Cells(1, fCol), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
        :=Array(1, 1), TrailingMinusNumbers:=True
    
'   Reformat column
    Columns(fCol).NumberFormat = "0"
        
    On Error GoTo 0
    Exit Sub
    
err_chk:
'   Return error message if PID not found
    If Err.Number = 91 Then
        MsgBox "Cannot find PID in row 4!", vbOKOnly, "ERROR!!!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
        
End Sub

Ooops...I have more than 1: PID & SumOfBudget & type. They are all on Row 4 though.
Sorry
 
Last edited:
Upvote 0
Ooops...I have more than 1: PID & SumOfBudget & tpre...
Are you saying that you have more than one column that you need to apply "Text to Columns" to?
 
Upvote 0
If my previous assumption is correct, then use this variation:
Code:
Sub MyTextToCol()

    Dim hdr As Variant
    Dim i As Long
    Dim fCol As Long
    
    Application.ScreenUpdating = False
    
'   Designate headers to look for
    hdr = Array([COLOR=#ff0000]"PID", "SumOfBudget", "tpre"[/COLOR])
                
    On Error GoTo err_chk
    
'   Loop through all values of array
    For i = LBound(hdr) To UBound(hdr)
    
'       Find which column "PID" is in in row 4
        fCol = Rows("4:4").Find(What:=hdr(i), After:=Range("A4"), LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Column
        
'       Perform text to columns on found column
        Columns(fCol).TextToColumns Destination:=Cells(1, fCol), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
            :=Array(1, 1), TrailingMinusNumbers:=True
    
'       Reformat column
        Columns(fCol).NumberFormat = "0"
        
    Next i
        
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub
    
err_chk:
'   Return error message if PID not found
    If Err.Number = 91 Then
        MsgBox "Cannot find " & hdr(i) & " in row 4!", vbOKOnly, "ERROR!!!"
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If
        
    Application.ScreenUpdating = True
        
End Sub
You would just update the part in red for all the column names that you want to apply it to.
 
Upvote 0
Hello All,
I have an access query which exports numbers as text. In the past, I would simply insert a column, then multiply the text number by 1, and drag the formula all the way down. Then copy and and paste as values back over the column with text numbers.
My question, is there a VBA code to select the entire column, and change all text numbers into real numbers...all at once?
If I use the recorder, all it tells me is when I select the cell, it doesn't show the step where I convert the error drop-down to number.
Thanks for the help

Just a point, there's an easier way to do this: type the number 1 in any empty cell. Select that cell and CTRL+C. Then select the column you want to convert into numbers, then Right-Click, then CLICK on Paste Special (don't just hover over it), then under Operation, select Multiply and hit OK.
 
Upvote 0

Forum statistics

Threads
1,224,043
Messages
6,176,045
Members
452,698
Latest member
MikaVmex

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