Consulting

Results 1 to 11 of 11

Thread: Separating values that are in a single cell

  1. #1

    Separating values that are in a single cell

    Hello,
    I need a macro that can separate values that are in a cell, separated by a space. For example, in a single cell I would have: 001234 12345 4456785 4545345

    Suppose I have many of those cells in column A. I need a macro to split those in other lines below the original cell (inserting lines).

    Now, a difficult part: Of the total amount of values in the original cell I only want to put in the lines below it the second, fourth, sixth (and so on) values.

    Is it possible?

  2. #2
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Try something like this...

    [VBA]Sub test()


    Dim str() As String
    Dim lrow As Long
    Dim iArrayCount As Integer
    lrow = 1
    While Sheet1.Cells(lrow, 1) <> ""
    str = Split(Sheet1.Cells(lrow, 1), " ")
    For iArrayCount = 0 To UBound(str)
    If iArrayCount > 0 Then
    Sheet1.Rows(lrow + iArrayCount).Insert
    End If
    Sheet1.Cells(lrow + iArrayCount, 1) = str(iArrayCount)
    Next iArrayCount
    lrow = lrow + UBound(str) + 1
    Wend

    End Sub[/VBA]

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Where do you get these inconsistent data from ? (NB. 'Structuring precedes coding')

  4. #4
    The numbers in the cell means a Shipping Number followed by its invoice, and there are many shipping numbers and their invoices in a single cell.

    CodeNinja, your macro works well, thanks, but gets all the values, and I need only the ones that would be the invoice (the second, the fourth, the sixth and so on). Is it possible to achieve that? Even if it is by deleting the undesired lines.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    and there are many shipping numbers and their invoices in a single cell.
    If they originate from a txt/csv file we'd better use those files than an Excel file.

  6. #6
    They don't, they come from a single cell from SAP

  7. #7
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by kurtwagner
    ...and I need only the ones that would be the invoice (the second, the fourth, the sixth and so on). Is it possible to achieve that? Even if it is by deleting the undesired lines.
    Hi there,

    Does that mean the value length is inconsistent? i.e., there may be six numbers in one record, four in another, and seven in another?

    Mark

  8. #8
    Quote Originally Posted by GTO
    Hi there,

    Does that mean the value length is inconsistent? i.e., there may be six numbers in one record, four in another, and seven in another?

    Mark
    Unfortunately, yes...

  9. #9
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    You had better posted a sample workbook !

    [vba]
    sub M_snb()
    sn=sheets(1).cells(1).currentregion

    for j=1 to ubound(sn)
    if sn(j,1)<>"" then
    sq=split(sn(j,1))
    for jj= 0 to ubound(sq)
    if jj mod 2=0 then sq(jj)=""
    next
    c01=c01 & " " & join(sq)
    end if
    next

    sn=split(trim(c01))
    cells(1,6).resize(ubound(sn)+1)=application.transpose(sn)
    End Sub
    [/vba]

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    As snb has suggested, an example workbook would likely result in better help. In .xls format would be preferable, and whilst the data can be fake, it should accurately correlate to what you are facing. That is - if there are occasional extra spaces, non-numeric values (123F123) that should be ignored, etc..., included, include these type oddities in the example.

    Mark

  11. #11
    VBAX Mentor Teeroy's Avatar
    Joined
    Apr 2012
    Location
    Sydney, Australia
    Posts
    414
    Location
    If I understand the problem correctly the following should work. It is developed from SNBs earlier code (sorry SNB had to replace your trademark variables with something I could remember as I went).

    [vba]Sub Split_Invoice_Number()
    Dim aOutput()
    vInput = Sheets(1).Cells(1).CurrentRegion
    If IsEmpty(vInput) Then Exit Sub
    For j = UBound(vInput) To 1 Step -1
    If vInput(j, 1) <> "" Then
    vTemp = Split(vInput(j, 1))
    ReDim aOutput(1 To (UBound(vTemp) / 2))
    For jj = 0 To UBound(vTemp)
    If jj Mod 2 > 0 Then aOutput((jj + 1) / 2) = vTemp(jj)
    Next
    End If
    Rows(j + 1 & ":" & j + (UBound(aOutput))).Insert
    Cells(j + 1, 1).Resize(UBound(aOutput)) = Application.Transpose(aOutput)
    Next
    End Sub
    [/vba]
    _________________________________________________________________________
    "In theory there is no difference between theory and practice. In practice there is." - Chuck Reid

    Any day you learn something new is a day not wasted.

Posting Permissions

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