Results 1 to 5 of 5

Thread: [RESOLVED] Loop in a Cell - Wrapped Cell

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    25

    Resolved [RESOLVED] Loop in a Cell - Wrapped Cell

    Hey Guys,

    I'm writing a VBA Macro which splits a long String in a lot of substrings and copies them in new Columns.
    My problem now is, that there is a Column called "Spare Parts" and in that Column there are only Cells which contain wrapped text.

    Name:  Unbenannt.jpg
Views: 243
Size:  42.6 KB

    The lines in the cells, except of the first 3, always look the same:
    ---> Spare Part ID(10Digits) , Spare Part Name , Number of Units , Price per Unit

    And I need to split those 4 parts into individual strings.
    I already managed to split those 4 parts and copy them in new Columns if there is only one String in a cell.(=No wrapped cell)

    But I dont know how to do that in a Wrapped Cell?
    I think I need to get the index of the position of the first "line break" character and save that value and then work with that ?

    Somebody has any ideas and maybe some code examples or something like that?

    Thank you guys in advance!
    Last edited by capk; Oct 26th, 2016 at 08:22 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Loop in a Cell - Wrapped Cell

    can you post a workbook (zip first) with a sample of the data

    probably split the cell on chr(10) (but hard to guess without some sample of the data), then loop the array and split each element to put the value into individual cells
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    25

    Re: Loop in a Cell - Wrapped Cell

    Here is a example with a sample of data.

    TestData.zip
    Attached Files Attached Files

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Loop in a Cell - Wrapped Cell

    As Pete says, you can split that long string by looking for the ascii code 10, like:

    Code:
    myNumber=asc(myChar)
    where myChar is the value of a single character when looping through the string.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Sep 2016
    Posts
    25

    Re: Loop in a Cell - Wrapped Cell

    As both of you guys said, I did the loop with Chr(10)

    Code:
    srcRow = 1
    dstRow = 1
    NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count
    
    For x2 = 1 To NumRows
        srcRow = srcRow + 1
        dstRow = dstRow + 1
        strLen = Len(Cells(srcRow, "B"))
        
    If strLen <> 0 Then
    test = Replace(Cells(srcRow, "B"), Chr(10), "", 1, -1, vbTextCompare)
    x = Len(test)
    anzLeer = strLen - x 
    rightLine = Cells(srcRow, "B").Value
    
        For o = 0 To anzLeer
        
            newLineLen = InStr(rightLine, Chr(10))
            leftLine = Left(rightLine, newLineLen)
            '#########SPLIT#########
            rightLine = Replace(rightLine, leftLine, "")
    
                If o = anzLeer Then
                    leftLine = rightLine
                End If
    
        MsgBox (leftLine)
        Next
    End If
    Next
    End Sub
    If you insert this in my previous uploaded sample, it will return every line in all cells one by one as I wanted.
    Now I only need to implement the rest of the code to split these lines i get returned.
    But thats the lesser Problem thank you guys for helping

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width