+ Reply to Thread
Results 1 to 19 of 19

Add a string between beginning and and of a chain per corresponding code

Hybrid View

  1. #1
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Add a string between beginning and and of a chain per corresponding code

    Find strings "BEGINNING" and "END" and add between them a string "BETWEEN" per every code.
    Notice that one code can have more than one chain (chain starts with string "BEGINNING", ends with string "END")
    Attached Files Attached Files

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,408

    Re: Add a string between beginning and and of a chain per corresponding code

    Put this formula in D1:

    =A1

    and this one in D2:

    =IF(OR(ISNUMBER(SEARCH("beginning",A2)),ISNUMBER(SEARCH("end",A2))),A2,IF(COUNTIF(A$1:A1,"*beginning")=COUNTIF(A$1:A1,"*end"),A2,A2&" between"))

    Copy the formula from D2 down as far as you need to.

    Hope this helps.

    Pete

  3. #3
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    Thank you AlphaFrog!!!
    Can I just put E1 (and E where column A is in code) if I want this to work for column E?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Add a string between beginning and and of a chain per corresponding code

    Sub Between()
        Dim v As Variant, i As Long, bBetween As Boolean
        With Range("A1", Range("A" & Rows.Count).End(xlUp))
            v = .Value
            For i = 1 To UBound(v, 1)
                If v(i, 1) Like "*BEGINNING" Then
                    bBetween = True
                ElseIf v(i, 1) Like "*END" Then
                    bBetween = False
                ElseIf bBetween Then
                    v(i, 1) = v(i, 1) & " BETWEEN"
                End If
            Next i
            .Value = v
        End With
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Add a string between beginning and and of a chain per corresponding code

    You're welcome and yes.

  6. #6
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    I tested program and noticed that it adds BETWEEN even for codes that don't have BEGINNING and END - if you rename cell A3 to CODE3 you will see it adds BETWEEN even thou CODE3 doesn't have BEGINNING and END

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Add a string between beginning and and of a chain per corresponding code

    Quote Originally Posted by percuk17 View Post
    I tested program and noticed that it adds BETWEEN even for codes that don't have BEGINNING and END - if you rename cell A3 to CODE3 you will see it adds BETWEEN even thou CODE3 doesn't have BEGINNING and END
    No need to PM me. I get notified, like you, when there is a reply to a thread I'm subscribed to.

    Try this...

    Sub Between()
        Dim v As Variant, i As Long, strCode As String
        With Range("A1", Range("A" & Rows.Count).End(xlUp))
            v = .Value
            For i = 1 To UBound(v, 1)
                If v(i, 1) Like "*BEGINNING" Then
                    strCode = v(i, 1)
                ElseIf v(i, 1) Like "*END" Then
                    strCode = ""
                ElseIf strCode Like v(i, 1) & "*" Then
                    v(i, 1) = v(i, 1) & " BETWEEN"
                End If
            Next i
            .Value = v
        End With
    End Sub

  8. #8
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    It worked again for the sample I submitted and I thank you for that.
    Obviously I didn't express my self well enough so that your code can work for every possible case. The problem is when BEGINNING is also interposal.
    My English is limited so I put down the case when code can not do the task.
    CODE1
    CODE1 BEGINNING
    CODE2
    CODE2 BEGINNING
    CODE1
    CODE1 END
    CODE1
    CODE1
    CODE1 BEGINNING
    CODE1
    CODE1
    CODE1
    CODE1 END
    CODE1
    CODE1
    CODE2
    CODE2
    CODE2
    CODE2
    CODE2
    CODE2
    CODE2
    CODE2 END
    CODE2
    CODE1
    CODE1 BEGINNING
    CODE1
    CODE1
    CODE1
    CODE1 END
    CODE1
    CODE1

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Add a string between beginning and and of a chain per corresponding code

    Maybe:

    Sub percuk17()
    Dim rcell As Range, x As Range, y As Range, z As Long
    z = Range("A" & Rows.Count).End(3).row
    For Each rcell In Range("A1:A" & z)
        If Right(rcell, 9) = "BEGINNING" Then
            Set x = Range(Cells(rcell.row + 1, 1), Cells(z, 1)).Find("END", LookIn:=xlValues, lookat:=xlPart)
                If Not x Is Nothing Then
                    For Each y In Range(Cells(rcell.row + 1, 1), Cells(x.row - 1, 1))
                        y = y & " BETWEEN"
                    Next y
                End If
            Set x = Nothing
        End If
    Next rcell
    End Sub

  10. #10
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    Works too!
    Thank you so much

  11. #11
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Add a string between beginning and and of a chain per corresponding code

    You're welcome. AlphaFrog's better though.

  12. #12
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Add a string between beginning and and of a chain per corresponding code

    Not sure what you want to happen when that occurs, does this help?

    Sub percuk17()
    Dim rcell As Range, x As Range, y As Range, z As Long, xx As String
    z = Range("A" & Rows.Count).End(3).row
    For Each rcell In Range("A1:A" & z)
        If Right(rcell, 9) = "BEGINNING" Then
            Set x = Range(Cells(rcell.row + 1, 1), Cells(z, 1)).Find("END", LookIn:=xlValues, lookat:=xlPart)
                If Not x Is Nothing Then
                    xx = Left(rcell, 5)
                    For Each y In Range(Cells(rcell.row + 1, 1), Cells(x.row - 1, 1))
                        If Left(y, 5) = xx Then
                        y = y & " BETWEEN"
                        End If
                    Next y
                End If
            Set x = Nothing
        End If
    Next rcell
    End Sub

  13. #13
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Add a string between beginning and and of a chain per corresponding code

    ?? Maybe:

    Sub percuk17z()
    Dim i As Long, rng As Range, rng2 As Range, x As Long, y As Range, z
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    ReDim z(1 To Range("A" & Rows.Count).End(3).row)
    For i = UBound(z) To LBound(z) Step -1
        If Left(Cells(i, "A"), 5) <> Left(Cells(i + 1, "A"), 5) Then Rows(i + 1).Insert
    Next i
    For Each textrange In Range("A2:A" & Range("A" & Rows.Count).End(3).row).SpecialCells(2, 2).Areas
        addr = textrange.Address(False, False)
        x = Range(addr).Item(1, 1).End(xlDown).row
        Set rng = Range(addr).Find("BEGINNING", LookIn:=xlValues, LOOKAT:=xlPart)
            If Not rng Is Nothing Then
                Set rng2 = Range(Cells(rng.row, 1), Cells(x, 1)).Find("END", LookIn:=xlValues, LOOKAT:=xlPart)
                    If Not rng2 Is Nothing Then
                        For Each y In Range(Cells(rng.row + 1, 1), Cells(rng2.row - 1, 1))
                            y = y & " BETWEEN"
                        Next y
                    End If
                Set rng2 = Nothing
            End If
        Set rng = Nothing
    Next textrange
    Range("A2:A" & Range("A" & Rows.Count).End(3).row).SpecialCells(4).EntireRow.Delete
    With Application
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True
    End With
    End Sub

  14. #14
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    Hey John,
    thanks again for working on this.
    I tried but it did only for CODE1 but not for CODE2 that has BEGINNING at cell A4 and END at cell A23

  15. #15
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    In addition codes (CODE1, CODE2...) could be something totally different, like EA, ANYTHING...

  16. #16
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    This is the resulting column

    CODE1
    CODE1 BEGINNING
    CODE2
    CODE2 BEGINNING
    CODE1 BETWEEN
    CODE1 END
    CODE1
    CODE1
    CODE1 BEGINNING
    CODE1 BETWEEN
    CODE1 BETWEEN
    CODE1 BETWEEN
    CODE1 END
    CODE1
    CODE1
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 BETWEEN
    CODE2 END
    CODE2
    CODE1
    CODE1 BEGINNING
    CODE1 BETWEEN
    CODE1 BETWEEN
    CODE1 BETWEEN
    CODE1 END
    CODE1
    CODE1

  17. #17
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Add a string between beginning and and of a chain per corresponding code

    I think I misinterpreted what you are looking for? Can you provide the expected results for Post #12.

  18. #18
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: Add a string between beginning and and of a chain per corresponding code

    Sub Between()
        Dim v As Variant, i As Long, strCode As String
        With Range("A1", Range("A" & Rows.Count).End(xlUp))
            v = .Value
            For i = 1 To UBound(v, 1)
                If v(i, 1) Like "*BEGINNING" Then
                    strCode = strCode & "|" & Split(v(i, 1), " ")(0) & "|"
                ElseIf v(i, 1) Like "*END" Then
                    strCode = Replace(strCode, "|" & Split(v(i, 1), " ")(0) & "|", "")
                ElseIf InStr(strCode, "|" & v(i, 1) & "|") Then
                    v(i, 1) = v(i, 1) & " BETWEEN"
                End If
            Next i
            .Value = v
        End With
    End Sub
    Last edited by AlphaFrog; 02-16-2017 at 12:18 PM.

  19. #19
    Registered User
    Join Date
    02-09-2017
    Location
    Atlanta, GA
    MS-Off Ver
    2010
    Posts
    11

    Re: Add a string between beginning and and of a chain per corresponding code

    Bingo! Totally works!
    Sorry for being confusing and thank you a million

+ 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. Replies: 1
    Last Post: 04-12-2014, 04:03 PM
  2. Need Help generating a and copying a chain of code.
    By borderpc in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-12-2013, 01:36 PM
  3. Move Text From End Of String To Beginning
    By Tellm in forum Excel General
    Replies: 4
    Last Post: 06-28-2013, 06:09 AM
  4. Replies: 5
    Last Post: 09-08-2010, 03:45 PM
  5. Replies: 3
    Last Post: 09-08-2010, 09:49 AM
  6. Add a string at the beginning and end
    By patyandra in forum Excel General
    Replies: 2
    Last Post: 12-05-2009, 07:05 PM
  7. Is there away to add a zero to the beginning of number string?
    By Anthony in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-15-2006, 06:55 PM

Tags for this Thread

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