+ Reply to Thread
Results 1 to 4 of 4

Splitting cell contents by lines

Hybrid View

oteixeira Splitting cell contents by... 05-21-2015, 12:16 PM
nilem Re: Splitting cell contents... 05-21-2015, 12:28 PM
oteixeira Re: Splitting cell contents... 05-21-2015, 12:37 PM
amphinomos Re: Splitting cell contents... 05-21-2015, 12:30 PM
  1. #1
    Forum Contributor
    Join Date
    06-12-2006
    Location
    Portugal
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    212

    Splitting cell contents by lines

    Hello to all,

    Column B contains many lines of numbers, as per the following example:
    Line1 111222/145251
    Line2 333115
    Line3 777555/154154
    Line4 888999/115116
    Line5 110110
    Line6 110115
    Line7 620260/145521

    So numbers are always 6 digits long, sometimes separated by "/". I need to pass all data to columd D, in sets of 6 digits. In the present case all need to obtain 11 lines of all sets of 6 digits, without the slash.

    I wrote a small peace of code but my experience in VBA is too bad to tell and i'm obtaining blank cells on column D:
    Sub parte()
    Dim linha As Integer
    Dim coluna As Integer
    Dim linhad As Integer
    Dim colunad As Integer
    Dim texto As String
    Dim temb As Boolean
    linha = 2
    coluna = 2
    linhad = 2
    colunad = 4
    
    For i = 1 To 20
        texto = Plan1.Cells(linha, coluna)
        temb = InStr(texto, "/")
        Select Case temb
            Case temb = True
            Plan1.Cells(linhad, colunad) = Mid(texto, 1, 6)
            Plan1.Cells(linhad + 1, colunad) = Mid(texto, 8, 6)
            linha = linha + 1
            linhad = linhad + 2
            Case temb = False
            Plan1.Cells(linhad, colunad) = Plan1.Cells(linha, coluna)
            linha = linha + 1
         End Select
    Next i
    
    End Sub
    Can someone please let me know what am i doing wrong? (besides writing an horrible code, i know that because i'm not a programmer)

    Many thanks in advance for any kind help.
    Octavio

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Splitting cell contents by lines

    Hi Octavio,
    try this
    Sub ertert()
    Dim x, y(), sp, i&, j&, k&
    x = Range("B1", Cells(Rows.Count, 2).End(xlUp)).Value
    ReDim y(1 To UBound(x) * 2, 1 To 1)
    For i = 1 To UBound(x)
        sp = Split(x(i, 1), "/")
        For j = 0 To UBound(sp)
            k = k + 1
            y(k, 1) = sp(j)
        Next j
    Next i
    Range("D1").Resize(k).Value = y()
    End Sub

  3. #3
    Forum Contributor
    Join Date
    06-12-2006
    Location
    Portugal
    MS-Off Ver
    Microsoft 365 Apps for enterprise
    Posts
    212

    Re: Splitting cell contents by lines

    Nilem, worked like a charm, i'm green with envy.
    Thanks a million.
    Octavio

  4. #4
    Forum Contributor
    Join Date
    05-30-2013
    Location
    France
    MS-Off Ver
    2010
    Posts
    260

    Re: Splitting cell contents by lines

    The Instr formula does not return a boolean (True/False) but the starting position of the string found. Try this instead :

        Select Case temb
            Case temb <> 0 
            Plan1.Cells(linhad, colunad) = Mid(texto, 1, 6)
            Plan1.Cells(linhad + 1, colunad) = Mid(texto, 8, 6)
            linha = linha + 1
            linhad = linhad + 2
            Case temb = 0
            Plan1.Cells(linhad, colunad) = Plan1.Cells(linha, coluna)
            linha = linha + 1
         End Select

+ 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. splitting multile lines in one cell into columns
    By m4rty5miff in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-11-2012, 08:53 AM
  2. splitting multiple lines in one cell into columns
    By m4rty5miff in forum Excel General
    Replies: 1
    Last Post: 02-10-2012, 12:15 PM
  3. Splitting cell contents
    By Jeshen in forum Excel General
    Replies: 6
    Last Post: 12-20-2011, 12:39 PM
  4. Splitting the contents of a cell
    By sheargraphix in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-14-2011, 07:11 AM
  5. Splitting cell contents
    By novodisc in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-12-2010, 04:49 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