Home » excel » vba – Split information at the : and push the 2nd part to another sheet in Excel

vba – Split information at the : and push the 2nd part to another sheet in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I can’t seem to split the information in these cells and put it to another sheet. Also I am probably doing this the hardest way possible. I currently use this code to cycle through all of the rows, find information with one value and send it to one sheet and with another value and send it to another sheet. Problem now is I want to only send part of the value. Here is my current code and an attempt I made.

j = 2
i = 2
k = 4
Do While ws.Cells(k, 1) <> ""
    If ws.Cells(k, 2) = "Data Not Found" Then
        Invalid.Cells(i, 1) = ws.Cells(k, 1)
        i = i + 1
    Else
         SEI.Cells(j, 1) = ws.Cells(k, 1)

         SEI.Cells(j, 2) = ws.Cells(k, 2)

         SEI.Cells(j, 3) = ws.Cells(k, 3)

         SEI.Cells(j, 4) = ws.Cells(k, 4)

         SEI.Cells(j, 5) = ws.Cells(k, 5)

         SEI.Cells(j, 6) = ws.Cells(k, 6)

         SEI.Cells(j, 7) = ws.Cells(k, 7)

         SEI.Cells(j, 8) = ws.Cells(k, 8)

        j = j + 1
    End If
    k = k + 1
Loop      

MY edit of the code to try and split it….THIS ISNT WORKING!, it doesnt cycle through and do B4, B5, B6…. I have done this before and it works so any advice would be great my guess its the formula I am using.

With SEI.Cells(j, 1)
            .Formula = ws.Cells(k, 1)
        End With
        With SEI.Cells(j, 2)
            .Formula = "=MID(INPUT!B4,SEARCH("":"",INPUT!B4)+1,LEN(INPUT!B4)-SEARCH("":"",INPUT!B4)+1)"
        End With
        With SEI.Cells(j, 3)
            .Formula = "=MID(INPUT!C4,SEARCH("":"",INPUT!C4)+1,8)"
        End With
        With SEI.Cells(j, 4)
            .Formula = ws.Cells(k, 4)
        End With
        With SEI.Cells(j, 5)
            .Formula = "=LEFT(INPUT!E4,SEARCH("":"",INPUT!E4)-1)"
        End With
        With SEI.Cells(j, 6)
            .Formula = "=LEFT(INPUT!F4,SEARCH("":"",INPUT!F4)-1)"
        End With
        With SEI.Cells(j, 7)
            .Formula = "=LEFT(INPUT!G4,SEARCH("":"",INPUT!G4)-1)"
        End With
        With SEI.Cells(j, 8)
            .Formula = "=LEFT(INPUT!H4,SEARCH("":"",INPUT!H4)-1)"
        End With
How to&Answers:

Untested:

EDIT: moved Split(), Trim() to a separate function:

Sub Tester()

    '....
    '.... 
    j = 2
    i = 2
    k = 4

    Do While ws.Cells(k, 1) <> ""
        If ws.Cells(k, 2) = "Data Not Found" Then
            Invalid.Cells(i, 1) = ws.Cells(k, 1)
            i = i + 1
        Else
            With SEI.Rows(j) 
                 .Cells(1).Value = ws.Cells(k, 1).Value
                 .cells(2).Value = GetValue(ws.cells(k, 2), 2)
                 .cells(3).Value = GetValue(ws.cells(k, 3), 2, 8)
                 .Cells(4).Value = ws.Cells(k, 4).Value
                 .cells(5).Value = GetValue(ws.cells(k, 5), 1)
                 .cells(6).Value = GetValue(ws.cells(k, 6), 1)
                 .cells(7).Value = GetValue(ws.cells(k, 7), 1)
                 .cells(8).Value = GetValue(ws.cells(k, 8), 1)
             End With
             j = j + 1
        End If
        k = k + 1
    Loop     
End Sub


Function GetValue(valIn, partNum As Long, Optional length As Long = 0)
    Dim rv
    rv = ""
    On Error Resume Next
    rv = Trim(Split(valIn, ":")(partNum - 1))
    On Error GoTo 0
    If Len(rv) = 0 Then rv = "???" 'comment out this line if you just want
                                   '    nothing returned if no content matched
    On Error GoTo 0
    If length > 0 Then rv = Left(rv, length)
    GetValue = Trim(rv)
End Function

Use of Split():

Split("A:B:C", ":")(0) is "A"
Split("A:B:C", ":")(1) is "B"
Split("A:B:C", ":")(2) is "C"