+ Reply to Thread
Results 1 to 7 of 7

VB Code to find certain text in column and copy digit part in diff column

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-31-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    193

    VB Code to find certain text in column and copy digit part in diff column

    Hello

    I have data as shown.
    I need VB COde to do following
    Look for 2 "#" in text in Column E.
    Example will be "06/07/21 Check #x1126 #9112"

    Now add last digit from #x1126 to #9112
    So you will have
    "06/07/21 Check #x1126 #91126"

    Now take digit part of #91126 and put in column D same row.

    I have manually done this as shown in yellow highlighted in column D.


    Please let me know if you have any questions.
    Thanks.

    R
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    Re: VB Code to find certain text in column and copy digit part in diff column

    Hi rmomin,

    Try below code based on your sample file ...
    Sub test()
    
    Dim a, Txt$
    a = Range("E2", Range("E" & Rows.Count).End(3))
    
    For x = 1 To UBound(a)
       If InStr(a(x, 1), "#") Then Txt = Split(a(x, 1), "#")(2) & Right(Application.Trim(Split(a(x, 1), "#")(1)), 1)
       a(x, 1) = Txt: Txt = ""
    Next
       
    [D2].Resize(UBound(a)) = a
    
    End Sub
    If I was able to help, you can thank me by clicking the * Add Reputation under my user name

  3. #3
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: VB Code to find certain text in column and copy digit part in diff column

    You can also try:

    Sub addfinalnum()
    Dim cel As Range
    Dim fst&, lst&, pos&
    Application.ScreenUpdating = False
    For Each cel In Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row)
        If InStr(cel, "#") > 1 Then
        fst = InStr(cel, "#")
        lst = InStrRev(cel, "#")
        pos = Mid(cel, fst + 5, 1)
        cel.Value = cel.Value & pos
        cel.Offset(, -1).Value = Mid(cel, lst + 1)
        End If
    Next
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Contributor
    Join Date
    01-31-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    193

    Re: VB Code to find certain text in column and copy digit part in diff column

    Hello:

    Thanks Nan and mani

    Works greak

    R

  5. #5
    Forum Contributor
    Join Date
    01-31-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    193

    Re: VB Code to find certain text in column and copy digit part in diff column

    Hello:

    Below is your code, i am using with no issue for data as shown in Sheet1

    But now have a look at Sheet2
    I need help to modify the code so that it splits out check# as shown

    Please let me know if you have any questions.
    Thanks.

    Riz
    Attached Files Attached Files

  6. #6
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: VB Code to find certain text in column and copy digit part in diff column

    Try

    Sub addfinalnum_Upda()
    Dim cel As Range
    Dim fst&, lst&, pos&
    Application.ScreenUpdating = False
    For Each cel In Range("E2:E" & Cells(Rows.Count, 5).End(xlUp).Row)
        If InStr(cel, "#") > 1 Then
        fst = InStr(cel, "#")
        lst = InStrRev(cel, "#")
            If fst <> lst Then
                pos = Mid(cel, fst + 5, 1)  'Remove this line if want it to work like example, not description
                cel.Value = cel.Value & pos 'Remove this line if want it to work like example, not description
                cel.Offset(, -1).Value = Mid(cel, lst + 1)
            ElseIf fst = lst Then
                cel.Offset(, -1).Value = Mid(cel, lst + 1)
            End If
        End If
    Next
    Application.ScreenUpdating = True
    End Sub
    Note: in your initial description, you outline two steps:
    Now add last digit from #x1126 to #9112
    So you will have
    "06/07/21 Check #x1126 #91126"

    Now take digit part of #91126 and put in column D same row.
    But your example only shows the second step. Should the outcome be like the description or your example? My code carries out the two steps in your description method.
    Last edited by maniacb; 06-09-2021 at 04:49 AM. Reason: added note

  7. #7
    Forum Contributor
    Join Date
    01-31-2012
    Location
    Georgia
    MS-Off Ver
    Excel 2010
    Posts
    193

    Re: VB Code to find certain text in column and copy digit part in diff column

    Hello

    Thank you Maniacb, this should work for me.
    Will contact if any issue or further enhancement needed.
    R

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VB Code to Find Digit part in Text and copy in different column
    By rmomin in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-02-2021, 10:34 AM
  2. Replies: 8
    Last Post: 02-17-2021, 09:18 AM
  3. Replies: 20
    Last Post: 05-10-2016, 06:05 PM
  4. Replies: 5
    Last Post: 10-23-2015, 12:35 PM
  5. Replies: 1
    Last Post: 05-21-2014, 05:05 AM
  6. [SOLVED] Find value in cells and copy part of it to other column
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2013, 04:59 AM
  7. Replies: 2
    Last Post: 01-13-2013, 06:50 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1