Consulting

Results 1 to 7 of 7

Thread: autonumber a string within a cell

  1. #1
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location

    Post autonumber a string within a cell

    Here is a sample value of a cell that contains my data

    "Z_DRAWING_NUMBER : SP2422-B50-NA2101-601&602-005 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-206 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-205 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-204 ; Z_SERIAL_NUMBER : JD85087203 - 6 ; Z_SERIAL_NUMBER : JD85087203 - 5 ; Z_SERIAL_NUMBER : JD85087203 - 4 ; Z_SERIAL_NUMBER : JD85087203 - 3 ; Z_SERIAL_NUMBER : JD85087203 - 2 ; Z_SERIAL_NUMBER : JD85087203 - 1 ; Z_MODEL_NUMBER : JD85087203 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-203 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-202 ; Z_TAG_NUMBER : B50-MS1-XFR(T)-201 ;"


    the string "Z_TAG_NUMBER" is repeating six times in the above cell contents. i want to extract out the z tag number to different cell. the output i want is "B50-MS1-XFR(T)-205 ; B50-MS1-XFR(T)-204 ; B50-MS1-XFR(T)-203 ; B50-MS1-XFR(T)-202 ; B50-MS1-XFR(T)-201 ;"

    i have tried a for loop to attach a number to the "Z_TAG_NUMBER" so i could run a text to column, but couldnt.

    to substitute Z_TAG_NUMBER with a value like $$1, $$2, $$3 (auto number with unusual special character) will help me to extract out the data.

    Thanks,
    Tharanipathy R

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test()
        Dim s As String
        Dim dic As Object
        Dim v
        Dim i As Long
        
        s = Range("a1").Value
        
        Set dic = CreateObject("scripting.dictionary")
        
        v = Split(s, ";")
        
        For i = LBound(v) To UBound(v)
            If Trim(v(i)) Like "Z_TAG_NUMBER :*" Then
                dic(i) = Trim(Split(v(i), ":")(1))
            End If
        Next
        
        If dic.Count > 0 Then
            Range("a3").Resize(dic.Count).Value = _
                WorksheetFunction.Transpose(dic.items)
        End If
                
    End Sub

  3. #3
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thanks a lot! its working!

  4. #4
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thanks a lot! its working! i need all the resulting components in a single row in a adjacent columns. if source is in a1, result should be in a b1, c1, d1, etc. so i can run the same program for a batch of data in "A" column!

  5. #5
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    since i have worked on it i wanted to post my solution.

    thank God, i used another object.

    Sub vbax_57448_find_substrings_in_a_string()
        Dim re_matches
        Dim i As Long
        
        With CreateObject("VBScript.RegExp")
            .Pattern = "Z_TAG_NUMBER\ \:\ B50\-MS1\-XFR\(T\)\-[0-9]+"
            .Global = True
            Set re_matches = .Execute(Range("A1").Value)
        End With
             
        For i = 1 To re_matches.Count
          Range("A1").Offset(i).Value = Replace(re_matches(i - 1), "Z_TAG_NUMBER : ", "")
        Next
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    Sub test2()
        Dim dic As Object
        Dim c As Range
        Dim v
        Dim r As Long
        Dim i As Long
        
        Set dic = CreateObject("scripting.dictionary")
        
        For Each c In Range("a1", Range("a" & Rows.Count).End(xlUp))
            v = Split(c.Value, ";")
            r = c.Row
            For i = LBound(v) To UBound(v)
                If Trim(v(i)) Like "Z_TAG_NUMBER :*" Then
                    dic(i) = Trim(Split(v(i), ":")(1))
                End If
            Next
            
            If dic.Count > 0 Then
                Range("b" & r).Resize(, dic.Count).Value = dic.items
            End If
            dic.RemoveAll
        Next
                
    End Sub

  7. #7
    VBAX Newbie
    Joined
    Oct 2016
    Posts
    5
    Location
    Thanks a lot! great work! fulfilled my expectations!

Posting Permissions

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