convert numbers stored as text, over 7 columns

JohnExcel222

New Member
Joined
Dec 19, 2018
Messages
35
Office Version
  1. 365
this works on column U.

How can I amend it to work on columns O to U ? I would like to convert numbers stored as text. Thanks

Sub texttoNum6() 'Excel VBA dynamic example of the text to numbers procedure.

Dim ar As Variant
Dim var As Variant
Dim i As Long
Dim lr As Long lr = Range("u" & Rows.Count).End(xlUp).Row ar = Range("u2:u" & lr)
ReDim var(1 To UBound(ar), 1 To 1)
For i = 1 To UBound(ar) 'Start of VBA loop var(i, 1) = ar(i, 1) * 1
Next i
Range("u2:u" & lr) = var
End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You do not need to loop through each cell. You can just loop through all the columns and use Text to Columns, like this:
VBA Code:
Sub MyNumberConvert()

    Dim c As Long
    
    Application.ScreenUpdating = False
    
'   Loop through columns O-U
    For c = 15 To 21
        Columns(c).TextToColumns Destination:=Cells(1, c), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
            :=Array(1, 1), TrailingMinusNumbers:=True
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub JohnExcel()
   With Range("O2:U" & Range("O" & Rows.Count).End(xlUp))
      .Value = .Value
   End With
End Sub
 
Upvote 0
I like Fluff's reply, it is a lot shorter and concise.
But just be aware that it may a need slight modification if your last row of data may have a blank in column O.
You would just need to change the "O" is this part:
VBA Code:
Range("O" & Rows.Count)
to some column that will always have a value in the last row of data.
 
Upvote 0
How about
VBA Code:
Sub JohnExcel()
   With Range("O2:U" & Range("O" & Rows.Count).End(xlUp))
      .Value = .Value
   End With
End Sub
How about
VBA Code:
Sub JohnExcel()
   With Range("O2:U" & Range("O" & Rows.Count).End(xlUp))
      .Value = .Value
   End With
End Sub
This give me a Debug failure :(
 

Attachments

  • Debug Failure.jpg
    Debug Failure.jpg
    13.1 KB · Views: 6
Upvote 0
I like Fluff's reply, it is a lot shorter and concise.
But just be aware that it may a need slight modification if your last row of data may have a blank in column O.
You would just need to change the "O" is this part:
VBA Code:
Range("O" & Rows.Count)
to some column that will always have a value in the last row of data.

Can you please give me the whole code? I did not understand
 
Upvote 0
You do not need to loop through each cell. You can just loop through all the columns and use Text to Columns, like this:
VBA Code:
Sub MyNumberConvert()

    Dim c As Long
   
    Application.ScreenUpdating = False
   
'   Loop through columns O-U
    For c = 15 To 21
        Columns(c).TextToColumns Destination:=Cells(1, c), DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
            Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
            :=Array(1, 1), TrailingMinusNumbers:=True
    Next c
   
    Application.ScreenUpdating = True
   
End Sub
This worked super :) Many thanks Joe.
 
Upvote 0
Oops, my code should have been
VBA Code:
Sub JohnExcel()
   With Range("O2:U" & Range("O" & Rows.Count).End(xlUp).Row)
      .Value = .Value
   End With
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,183
Members
448,872
Latest member
lcaw

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