Delimiting the text in a cell to column vertically

In this article, we will create a macro to delimit text in a cell to column vertically. We can delimit the text in the cell based on the delimiter. Delimiter can either be a comma, space, backslash etc.

To explain the working of the macro, we have taken URL as sample text in cell A10. Macro can be executed by clicking the “Submit” button.

ArrowSampleData

After executing the macro, it will return output in the next column. Text value in the cell is separated to multiple sub strings in multiple cells in the second column.

ArrowAfterRunningMacro

Logic explanation

We have created a macro “texttocolumns” which performs two tasks. Firstly, it delimits the string in the A10 to multiple sub strings, based on the delimiter. In this example, we have used dash (-) as delimiter. Secondly, it traverses generated sub strings from multiple columns to a single column.

Code explanation

Rng.texttocolumns Destination:=Rng.Offset(0, 1), DataType:=xlDelimited, _

TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Other:=True, OtherChar _

:="-"

Above code is used to delimit the string in the range “Rng”. Destination specifies the location range where output will appear. DataType specifies the type of delimiter used. TextQualifier specifies the character which is used for text qualifier. OtherChar specifies the character used as delimiter.

 

Please follow below for the code

Sub texttocolumns()

'Declaring variables
Dim StartRow, i, LastCol As Long
Dim Rng As Range

'disabling the display alerts
Application.DisplayAlerts = False

'Initializing the variable
StartRow = 10
Set Rng = Range("A10")

'Separting the text based on the delimiter
Rng.texttocolumns Destination:=Rng.Offset(0, 1), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Other:=True, OtherChar _
    :="-"

'Finding the column number of cell containing the last string after delimiting
LastCol = Rng.End(xlToRight).Column

'arranging the text in columns to rows
For i = 2 To LastCol
    Cells(10, i).Cut Cells(StartRow, 2)
    StartRow = StartRow + 1
Next i


End Sub

If you liked this blog, share it with your friends on Facebook. Also, you can follow us on Twitter and Facebook.

We would love to hear from you, do let us know how we can improve our work and make it better for you. Write to us at info@exceltip.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.