• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

For Each Worksheet Text to Columns Loop Issue

chirayu

Well-Known Member
Hi All,

For some reason my code is not working on each sheet in the workbook.

It throws up a popup box saying do I want to replace contents & then only works on the activesheet and ends.

Code:
Sub Macro1()
 
  If ActiveSheet.Name = "Home" Then
  End
  Else
  Dim ws As Worksheet
  For Each ws In ActiveWorkbook.Worksheets
  With ws
  Columns("A:A").Select
  Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
  FieldInfo:=Array(Array(0, 1), Array(35, 1), Array(48, 1)), TrailingMinusNumbers _
  :=True
  Range("A1").Select
  End With
  Next ws
  End If
 
End Sub
 
Remember that when using 'with' statements, need to include a period in front of objects so that they trace back to With object. Otherwise, they will use ActiveSheet. Both "Columns" and "Range" are missing the leading periods.
Corrected code:
Code:
Sub Macro1()
Dim ws As Worksheet
If ActiveSheet.Name = "Home" Then
    End
Else
    For Each ws In ActiveWorkbook.Worksheets
        With ws
            'Since we're not selecting the sheet (nor do we want to)
            'don't try to select range
            .Columns("A:A").TextToColumns Destination:=.Range("A1"), DataType:=xlFixedWidth, _
                FieldInfo:=Array(Array(0, 1), Array(35, 1), Array(48, 1)), TrailingMinusNumbers _
                :=True
        End With
    Next ws
End If

End Sub
 
Back
Top