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")
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")
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
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?
![]()
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.
You're welcome and yes.
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
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
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
Works too!
Thank you so much
You're welcome. AlphaFrog's better though.
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
?? 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
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
In addition codes (CODE1, CODE2...) could be something totally different, like EA, ANYTHING...
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
I think I misinterpreted what you are looking for? Can you provide the expected results for Post #12.
![]()
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.
Bingo! Totally works!
Sorry for being confusing and thank you a million
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks