Consulting

Results 1 to 4 of 4

Thread: Text to columns for every character in text file

  1. #1
    VBAX Regular
    Joined
    Sep 2017
    Posts
    24
    Location

    Text to columns for every character in text file

    Hi

    I need VBA code to import text file and put it in the format of

    1. No spaces
    2. Every character in a new column
    3. Ignore commas, full stops

    As the text to columns 'fixed width' function in excel but saves having to click every column to the end of the text


    Thank you

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    After importing the file, you can record a macro if you are unsure how to do that. You will have the data all in column A. Then you can use this code to split each character.

    Dim i                   As Long
    Dim j                   As Long
    Dim n                   As Long
    Dim LastRow             As Long
    
    
        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        For i = 1 To LastRow
            n = Len(Range("A" & i).Text)
            For j = 1 To n
                If Mid(Range("A" & i).Text, j, 1) <> " " Then
                    Cells(i, Columns.Count).End(xlToLeft).Offset(0, 1).Value = Mid(Range("A" & i).Text, j, 1)
                End If
            Next j
        Next i

  3. #3
    Like this?
    Sub Hola()
    Dim i As Long, c As Range
    Application.ScreenUpdating = False
    For Each c In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    c.Value = Replace(Replace(Replace(c, ",", ""), ".", ""), Chr(32), "")
        For i = 1 To Len(c)
            Cells(c.Row, i + 1) = Mid(c, i, 1)
        Next i
    Next c
    Application.ScreenUpdating = True
    End Sub

  4. #4
    VBAX Regular
    Joined
    Sep 2017
    Posts
    24
    Location
    Great thanks both!

    Works a treat :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •