Text to column in Excel

Text to column in Excel
Create a professional-looking website today!

Text to column:

Excel offers a built in function which can convert a running string into columns based on delimiter. In this article we will see how we can achieve text to column conversion. If you want to try manually please follow given steps:

  • Select range or cells contain text that you would like to convert into columns (refer figure “rawData’)
  • Locate Date ribbon tab
  • Click on Text to Column button under Data Tools group (refer figure 1.0)
  • A popup comes select Delimited (refer figure 1.1)
  • Under delimiter group check Other and type hyphen (“-“) you can put your own delimiter as per your string. As soon as you put the hyphen the preview changes by which you can determine the output.
  • Click Next and it will prompt for choose column data formatting (refer figure 1.2)
  • Say Finish and your text is converted into column as shown in figure 1.3
rawData

Text to Column dialog:

figure 1.0

Delimiter settings:

figure 1.1

Set column data types:

figure 1.2

Finally data is converted as below:

figure 1.3

Let’s put some code to automate the same task. Point to be take we are using VSTO .Net C# and VB style code in below example hence following prerequisites are advised:

  1. Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  2. Create Excel Addin in C# or VB code style (Visual Studio Tools for Office)
  3. Excel 2010 or above
  4. Create a ribbon designer and put button

C# code example:

private void btnTextToColumn_Click(object sender, RibbonControlEventArgs e)
{
	//Retain selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;

	//Get active sheet reference
	excel.Worksheet oWorksheet = Globals.ThisAddIn.Application.ActiveSheet;
	
	//Set a target range
	excel.Range oTargetRange = oWorksheet.Range["A1:A3"];

	//Put query to convert text to column
	oRange.TextToColumns(oTargetRange, excel.XlTextParsingType.xlDelimited,
		excel.XlTextQualifier.xlTextQualifierDoubleQuote, false, false, false, false, false, true, "-");
}

VB.Net code example:

Private Sub btnTextToColumns_Click(sender As Object, e As RibbonControlEventArgs) Handles btnTextToColumns.Click
	'Retain selection
	Dim oRange As excel.Range
	oRange=Globals.ThisAddIn.Application.Selection

	'Get active sheet reference
	Dim oWorksheet As excel.Worksheet
	oWorksheet=Globals.ThisAddIn.Application.ActiveSheet

	'Set a target range
	Dim oTargetRange As excel.Range
	oTargetRange=oWorksheet.Range("A1:A3")

	'Put query to convert text to column
	oRange.TextToColumns(oTargetRange, excel.XlTextParsingType.xlDelimited,
			excel.XlTextQualifier.xlTextQualifierDoubleQuote, false, false, false, false, false, true, "-")
End Sub

VBA code example:

Public Sub TextToColumn()
    'get selection into range
    Dim oRange As Range
    Set oRange = Selection
    
    'get active sheet reference
    Dim oWorksheet As Worksheet
    Set oWorksheet = ActiveSheet
    
    'get target range
    Dim oTargetRange As Range
    Set oTargetRange = oWorksheet.Range("A1:A3")
    
    'Fire query to perform text to column conversion
    oRange.TextToColumns Destination:=oTargetRange, DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="-"
End Sub

Next >> Remove duplicates from Excel

Leave a Reply

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