Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 22

Thread: Solved: split line in batchs

  1. #1
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location

    Exclamation Solved: split line in batchs

    hi all...

    i need to split a line into batchs

    from 23C10L1P in location c5
    from 13C9L1P in location d5
    from 17C8L1P in location e5

    to

    23 in location d5
    10 in location e5
    1 in location f5

    got the first part by using =left(c5,find("c")-1) which gave me 23
    the second and last part is a total lost to me.


    thanks and regards

    cckfm2000

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hi,

    If it will always be that structure, you could just use ..

    =MID(A1,6,2)&" "&TRIM(RIGHT(A1,FIND(" ",A1,6)+1))

  3. #3
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    cckfm2000,

    If you'd be open to using a user-defined function, you could add the following to a standard module of the workbook you need this in:[vba]Function ExtractNumbers(ByVal FullString As String, ByVal WhichNumber As Long) As Long
    Dim RegEx As Object, RegC As Object
    Set RegEx = CreateObject("vbscript.regexp")
    RegEx.Pattern = "\d+"
    RegEx.Global = True
    If RegEx.Test(FullString) Then
    Set RegC = RegEx.Execute(FullString)
    If WhichNumber <= RegC.Count Then
    ExtractNumbers = RegC(WhichNumber - 1)
    Exit Function
    End If
    End If
    Set RegEx = Nothing
    Set RegC = Nothing
    End Function[/vba]That way, you'd only need to use a formula like:
    =EXTRACTNUMBERS(C5,1) to get 23
    =EXTRACTNUMBERS(C5,2) to get 10
    =EXTRACTNUMBERS(C5,3) to get 1
    if the number doesn't exist ( =EXTRACTNUMBERS(C5,4) ) then a zero is returned.

  4. #4
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    thanks for the reply..

    with the user-defined all i get #name?

    back to the drawing board for me...

  5. #5
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Did you put it in a standard module in the workbook you're calling it from? ie if you're calling it from book1.xls, did you put it in a module in book1, or in an installed addin? Either would allow it to work, however if you put it in personal.xls it would not.

  6. #6
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    kept getting the error when i was using excel 97 sr-2 but just had a go in excel 2000 and it works

    magic... wow thats looks very good..

    thanks

  7. #7
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Makes sense, I didnt think about version differences..
    xl97 should have been able to use udf's (dont quote me on that), but I don't believe the vbscript/regexp was available in 97. If you need something for 97 we could write that too, though it wouldnt be as sleek as the regexp version

    Let us know if you need anything else!

  8. #8
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    thanks alot mvidas you've been a star today.

  9. #9
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Um, why exactly would one want to use a UDF with RegExp instead of a native formula combination?? Especially one so fast!?!

  10. #10
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Zack,

    Native functions would have been better, I was just under the assumption the letters in the cells may change. If There will always be a C,L,P in them then these formulas would work:

    1st number =LEFT(C5,FIND("C",C5)-1)
    2nd number =MID(C5,FIND("C",C5)+1,FIND("L",C5)-FIND("C",C5)-1)
    3rd number =MID(C5,FIND("L",C5)+1,FIND("P",C5)-FIND("L",C5)-1)

    I thought the regexp version would be more versatile for different situations though. But if the cells will always have the same format, a UDF wouldn't be necessary.

    I couldn't get yours to work, I may have misinterpreted A1 though..
    Matt

  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Copy this into A1:A3...

    from 23C10L1P in location c5
    from 13C9L1P in location d5
    from 17C8L1P in location e5

    Then put this in B1...

    =MID(A1,6,2)&" "&TRIM(RIGHT(A1,FIND(" ",A1,6)+1))

    .. and copy down.

    Not dependent on specific characters, but it is dependent on the structure.

    And I see why you chose a UDF, makes sense now.

  12. #12
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Ha! Now I definately feel the "you've been a star today" is wrong I read
    Quote Originally Posted by post before this one
    from 23C10L1P in location c5
    from 13C9L1P in location d5
    from 17C8L1P in location e5

    to

    23 in location d5
    10 in location e5
    1 in location f5
    and interpreted it as
    Quote Originally Posted by my head
    23C10L1P is in cell c5
    13C9L1P is in cell d5
    17C8L1P is in cell e5

    to

    23 needs to go to cell d5
    10 needs to go to cell e5
    1 needs to go to cell f5
    D'oh!

  13. #13
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    using firefytr function
    =MID(A1,6,2)&" "&TRIM(RIGHT(A1,FIND(" ",A1,6)+1))
    i get #value

    but using mvidas function

    1st number =LEFT(C5,FIND("C",C5)-1)
    2nd number =MID(C5,FIND("C",C5)+1,FIND("L",C5)-FIND("C",C5)-1)
    3rd number =MID(C5,FIND("L",C5)+1,FIND("P",C5)-FIND("L",C5)-1)

    i get what i need

  14. #14
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Did you change A1 in Zack's function to C5?

  15. #15
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    yes

  16. #16
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hmm, interesting. Can you post a sample spreadsheet with a few cells containing your data set? I'm rather intrigued now..

  17. #17
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    here it is...

  18. #18
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    CCkfm,

    His function was designed to work on cells that have strings like "from 23C10L1P in location c5" (no quotes), as you described in your first post, and outputs strings like "23 in location c5".

    Matt

  19. #19
    VBAX Tutor CCkfm2000's Avatar
    Joined
    May 2005
    Posts
    209
    Location
    sorry sorry sorry i should have made myself more understanding in what i needed.

    i still got what i wanted thanks to all for all the help

  20. #20
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Ah! I see! I was misunderstaning you.

Posting Permissions

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