+ Reply to Thread
Results 1 to 3 of 3

Text to Row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Text to Row

    I am using the code below to convert text in selected cells in one row to other rows based on a specified delimiter (e.g. vbLF). It works fine for selected cells in a single row, but wondering what I need to do to have it work for selected cells on multiple rows? Thanks.

    Sub Split2Rows()
    'Split data in a cell based on inputted delimiter
        Dim arr As Variant
        Dim c As Range, Rng As Range
        Dim j As Long ' variable to iterate over the arr
        Dim RowN As Long ' number of rows to add
        Dim DCount As Long ' Delim count for active cell
        Dim Delim As String  'delimiter to split
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        On Error Resume Next
        Delim = InputBox("Enter Delimiter to Split" & vbLf & "or Ok for Carriage Return", , vbLf)
        DCount = Len(ActiveCell) - Len(Replace(ActiveCell, Delim, ""))
        RowN = InputBox("Enter number of rows to add", , DCount)
        Set Rng = Selection
        If RowN = 0 Then GoTo Skip
        Range(ActiveCell.Offset(1, 0), ActiveCell.Offset(RowN, 0)).Select
            Selection.EntireRow.Insert
    Skip:
    
        ' iterating over all cells in Rng
    
        
            For Each c In Rng
    
            ' split function splits each cells content to an array
             arr = Split(c, Delim)  'split on inputted delim
    
            ' iterating over the array of split strings
            For j = LBound(arr) To UBound(arr)
    
                ' assigning the separated values to rows
                 If c.Offset(RowN, 0).Value <> vbNullString Then Exit Sub
                 c.Offset(j, 0) = arr(j)
            Next j
        Next c
        Selection.Rows.AutoFit
        Rng.Rows.AutoFit
    
         Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,345

    Re: Text to Row

    For cells in a single column:

    Sub Split2Rows()
        'Split data in a cell based on delimiter
        Dim arr As Variant
        Dim Rng As Range
        Dim lRow As Long
        Dim j As Long ' variable to iterate over the arr
        Dim Delim As String  'delimiter to split
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        On Error Resume Next
        
        Delim = vbLf
        
        Set Rng = Selection
        
        For lRow = Cells(Rows.Count, Rng.Column).End(xlUp).Row To Rng.Cells(1).Row Step -1
          
            ' split function splits each cells content to an array
            arr = Split(Cells(lRow, Rng.Column), Delim)  'split on inputted delim
            ' iterating over the array of split strings
            Cells(lRow, Rng.Column).Value = arr(0)
            For j = 1 To UBound(arr)
                ' assigning the separated values to rows
                If Cells(lRow, Rng.Column).Offset(j, 0).Value <> "" Then
                    Cells(lRow, Rng.Column).Offset(j, 0).EntireRow.Insert
                End If
                Cells(lRow, Rng.Column).Offset(j, 0) = arr(j)
            Next j
        Next lRow
        Selection.Rows.AutoFit
        Rng.CurrentRegion.Rows.AutoFit
        
        Application.ScreenUpdating = True
        Application.DisplayAlerts = True
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Text to Row

    Thanks Bernie.. That will work..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to Mod Code on red text, I have VBA to distinguish Paid(Black Text) Unpaid(Red Text)
    By Garbology in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-11-2014, 07:55 PM
  2. [SOLVED] function IF to return text based if text contains exact text
    By in nomine noctis in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-05-2013, 06:25 AM
  3. Replies: 0
    Last Post: 04-22-2013, 12:13 PM
  4. Macro to Parse Text - Import text to Excel from Multiple Text Files & Folders
    By Novice_To_Excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2012, 01:05 AM
  5. [SOLVED] Use .text from previous text box in form to prefill text in second text box
    By chromachem in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2012, 10:04 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1