TextToColumns deleting contents after cells with no delimiter

blueangel2323

New Member
Joined
Jul 12, 2011
Messages
15
Hi everyone, I have a big problem here and it would be greatly appreciated if anyone could help me.

Let's say that I have the following code:
Code:
Columns("A:A").TextToColumns Destination:=Range("A1")
and I'm using commas as delimiters. I run the script on the following data:
Code:
A2: Gobble,Turkey,Dinner     B2: (blank)     C2: (blank)
A3: Gobble                   B3: Hello       C3: Everybody

The results would look something like this:
Code:
A2: Gobble     B2: Turkey     C2: Dinner
A3: Gobble     B3: (blank)    C3: (blank)

The first line (row 2) is fine, that's exactly what TextToColumns is supposed to do.

The problem is with the second line (row 3). The cells after A3 were replaced by blank cells, even though I want to keep "Hello" and "World"

Is there a way to tell the script not to replace cells that are already occupied, or better yet, to skip cells (in column A) that have no delimiter?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
There's no way to change the functionality of the built-in TextToColumns method. You would need to write a custom macro to do so. Probably loop through each cell in column A, check if there is a delimiter, check if there's already text in adjacent columns and skip, etc.
 
Upvote 0
As a work around,

If your data is consistent (always has two delimiters) then you could insert columns and delete the empty cells
 
Upvote 0
Try this code:

IMPORTANT: DO SOME TESTS WITH A COPY OF YOUR WORKBOOK.

Code:
Sub TextColumns()
    Dim myCell As Range
    Dim myMax, myComma, myRow As Long
 
    For Each myCell In Range("A:A")
        myComma = Len(myCell) - Len(Replace(myCell, ",", ""))
        If myMax < myComma Then
            myMax = myComma
        End If
        If myCell.Value = "" Then
            myRow = myCell.Row - 1
            Exit For
        End If
    Next myCell
 
    Range(Cells(1, 2), Cells(1, myMax + 1)).EntireColumn.Insert
 
    Columns("A:A").TextToColumns _
        Destination:=Cells(1, 1), _
        DataType:=xlDelimited, _
        Comma:=True
 
    Range(Cells(1, 1), Cells(myRow, myMax + 1)). _
        SpecialCells(xlCellTypeBlanks).Select
 
    Selection.Delete Shift:=xlToLeft
End Sub

Markmzz
 
Upvote 0
Here is a macro which should produce the end results you are looking for..
Code:
Sub TTC()
  Dim X As Long, Arr() As String
  Const StartRow As Long = 2
  Const SearchColumn As String = "A"
  Const Delimiter As String = ","
  For X = StartRow To Cells(Rows.Count, SearchColumn).End(xlUp).Row
    Arr = Split(Cells(X, SearchColumn).Value, Delimiter)
    If UBound(Arr) > 0 Then Cells(X, SearchColumn).Resize(, UBound(Arr) + 1) = Arr
  Next
End Sub
 
Upvote 0
Here is a macro which should produce the end results you are looking for..
Code:
Sub TTC()
  Dim X As Long, Arr() As String
  Const StartRow As Long = 2
  Const SearchColumn As String = "A"
  Const Delimiter As String = ","
  For X = StartRow To Cells(Rows.Count, SearchColumn).End(xlUp).Row
    Arr = Split(Cells(X, SearchColumn).Value, Delimiter)
    If UBound(Arr) > 0 Then Cells(X, SearchColumn).Resize(, UBound(Arr) + 1) = Arr
  Next
End Sub

That works perfectly, thank you so much! :)
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
Members
452,915
Latest member
hannnahheileen

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