Consulting

Results 1 to 11 of 11

Thread: Separating piece of file names into three different column

  1. #1
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location

    Thumbs up Separating piece of file names into three different column

    How do I separate the following file name using the formula?

    Apr_27_2014_12_54_43_PM_Area3_West. S._12.xls

    I want Area 3 in one column, West. S. in second column and 12 in third column. I know this is doable. I have seen the formula before but can't find that file to copy the formula.

    Thank you so much!

  2. #2

  3. #3
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    If you know the position, I made this UDF like the Field() in L123. Obviously, you need to set the 3rd parameter value as "_".

    '=Field(A1, 2)
    Function Field(cell, pos As Integer, Optional delimit As String = " ")
      Dim a() As String
      a() = Split(cell, delimit)
      Field = a(pos - 1)
    End Function

  4. #4
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location
    Thank you for this. I have a long list. The 2nd and 3rd numbers of the formula is not always same. For example, I can't use same formula for "Apr_20_2014_11_06_43_AM_DSU1_Renn. T._9.xls" I am looking for one formula that i can drag for my long spreadsheet.

  5. #5
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location
    is this doable without macro?

  6. #6
    Yes it is doable without a macro, but it's also handy to have the complete details in the first place

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The UDF is better, but

    B2:=LEFT(MID(A2,25,99),FIND("_",MID(A2,25,99))-1)

    C2:=LEFT(SUBSTITUTE(MID($A2,25,99),B2&"_",""),FIND("_",SUBSTITUTE(MID($A2,2 5,99),B2&"_",""))-1)

    D2:=LEFT(SUBSTITUTE(MID($A2,25,99),B2&"_"&C2&"_",""),FIND(".xls",SUBSTITUTE (MID($A2,25,99),B2&"_"&C2&"_",""))-1)
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    in B1: =LEFT(MID(A1,25,LEN(A1)),FIND("_",A1,25)-25)
    in C1: =MID(A1,26+LEN(B1),SEARCH("_",A1,26+LEN(B1))-26-LEN(B1))
    in D1: =LEFT(RIGHT(A4,LEN(A4)-LEN(C6&C8)-26),FIND(".",A4,27+LEN(C6&C8))-27-LEN(C6&C8))
    Last edited by snb; 05-07-2014 at 12:53 AM.

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    All of the 4 methods posted work for the first string except for mine. Mine needed an extra function to parse out the number part for the last field.

    Ashely's method fails for the 2nd string because you did not use a consistent file naming convention.

    My numberpart UDF is:
    Function NumberPart(aString As String) As Long
      Dim s As String, i As Integer, mc As String
      For i = 1 To Len(aString)
        mc = Mid(aString, i, 1)
        If Asc(mc) >= 48 And Asc(mc) <= 57 Then s = s & mc
      Next i
      NumberPart = CLng(s)
    End Function
    That UDF works in this case but might not be optimal for other scenarios.

    I have attached a test file link showing the 4 methods so you can test other strings easily. The attachment feature of this forum has not been working for me. https://app.box.com/s/dx239343lhwby0zgg5l6

  10. #10
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or

    in C9:E9 =F_snb($A$9,column())

    Function F_snb(c00, y)
        F_snb = Split(c00, "_")(UBound(Split(c00, "_")) + y - 5)
        If y = 5 Then F_snb = Val(F_snb)
    End Function
    If you want my formula treat numbers as number:

    in D1: =--LEFT(RIGHT(A4,LEN(A4)-LEN(C6&C8)-26),FIND(".",A4,27+LEN(C6&C8))-27-LEN(C6&C8))

  11. #11
    VBAX Regular
    Joined
    Oct 2009
    Posts
    69
    Location
    All of you are simply awesome out here. Thank you so much! Yes, all of these methods are working well on my long spreadsheet. Ken, thank you so much for the test file. Thank you everyone!

Posting Permissions

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