Looping through Columns using for next

Mozzz

Board Regular
Joined
May 30, 2011
Messages
66
I need to loop through columns 5 to LastColumn where LastColumn = Cells(1,Columns.Count).End(xlToLeft). Columns. I need to select each one individually so that I can convert the Text to a number using Text to column.

Here is what I tried along with a few other changes, nothing of course which worked
:
Code:
  For i = 5 To LastColumn
'        Columns(I:I)) _
            .TextToColumn Destination:=Range(i, & 1), _
'            DataType:=xlDelimited, TextQualifer:=xlDoubleQuote, _
'            Tab:=True, FieldInfo:=Array(1, 1)
'    Next i
Help Please
 
Sorry so long getting back. Here is what finally worked for me.

Code:
  With Selection
            .TextToColumns Destination:=ActiveCell.Range("A1")
            .NumberFormat = "0.00"
        End With

I really appreciate all the help and feel bad that I was not very punctual in thanking you.

Mozzz
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
For a different approach, I think this macro will do what you want (just assign your column letters where indicated)...
Code:
Sub MakeNumbersIntoNumbers()
  Dim V As Variant
  Const ColumnsToConvert = "B:C,E,H,M:Z,AA,AE"
  Application.ScreenUpdating = False
  For Each V In Split(ColumnsToConvert, ",")
    With Intersect(ActiveSheet.UsedRange, Columns(V))
      .Value = .Value
    End With
  Next
  Application.ScreenUpdating = True
End Sub

The assumption is that none of those columns are formatted as Text.

Oh, and yes, the .Value=.Value statement is intentional... as a matter of fact, that is the line doing all the work.
 
Last edited:
Upvote 0
Thanks for the suggestion, I am a little leary of Assigning the Columns, this is what seems to work best for me. I am trying (maybe too much) to make this as dynamic as possible.

Code:
'   Converts Text to Numbers
    
    For i = 5 To LastColumn + 1
        If Application.CountA(Columns(i)) > 0 Then
            Columns(i).Select
            
        With Selection
            .TextToColumns Destination:=ActiveCell.Range("A1")
            .NumberFormat = "0.00"
        End With
                    
        ActiveCell.Offset(0, 1).Columns("A:A").EntireColumn.Select
            
        End If
    Next i
 
Upvote 0
Okay, if that looping structure works for you, then use it to surround my .Value=.Value approach...
Code:
    For i = 5 To LastColumn + 1
        If Application.CountA(Columns(i)) > 0 Then
            With Intersect(ActiveSheet.UsedRange, Columns(i))
               .Value = .Value
            End With
        End If
    Next

Again, the assumption is that none of those columns are formatted as Text.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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