Horizontal cycle (VBA)

Fixed

Board Regular
Joined
Apr 28, 2017
Messages
95
Hello!

I have some macro:

Code:
Sub bracketsSub()
    Dim arr()   As Variant
    Dim x       As Long
    
    x = Cells(Rows.Count, 1).End(xlUp).Row
    arr = Cells(1, 1).Resize(x, 2).Value
    
    For x = LBound(arr, 1) To UBound(arr, 1)
        arr(x, 2) = brackets(CStr(arr(x, 1)))
    Next x
    Cells(1, 1).Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr
    Erase arr
End Sub
Private Function brackets(ByRef sss As String) As String
Dim aaa As Long '---
Dim bbb As Long '---
On Error GoTo Error_handler:
     aaa = InStr(sss, "(")
     bbb = InStr(aaa, sss, ")")
     brackets = Mid(sss, aaa + 1, bbb - aaa - 1)
Exit Function
Error_handler:
brackets = "-no-"
End Function

It's extract some text (first found text) from the brackets "()" from the strings in the "A" column and copy to "B" column.
What I should do to extract all texts from the brackets?

Example I want to get:

A
B
C
D
1
(yes)(no)(don't know)<strike></strike>
yes
no
don't know<strike></strike>
2
(yes)(no)<strike></strike>
yes
no
-no-
3
(yes)<strike></strike>
yes
-no-<strike></strike>
-no-<strike></strike>

<tbody>
</tbody>

Help me please!
 
This works for both, but it will be limited depending how far you move the goal posts.!!!
Code:
[COLOR=navy]Sub[/COLOR] MG27Oct53
[COLOR=navy]Dim[/COLOR] Rng [COLOR=navy]As[/COLOR] Range, Dn [COLOR=navy]As[/COLOR] Range, Txt [COLOR=navy]As[/COLOR] [COLOR=navy]String,[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Sp [COLOR=navy]As[/COLOR] Variant, Mch
[COLOR=navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
Mch = Array("yes", "no", "don'[COLOR=green][B]t know")[/B][/COLOR]
[COLOR=navy]For[/COLOR] [COLOR=navy]Each[/COLOR] Dn [COLOR=navy]In[/COLOR] Rng
    Dn.Offset(, 1).Resize(, 3) = "-no-"
        [COLOR=navy]For[/COLOR] n = 0 To 2
            [COLOR=navy]If[/COLOR] InStr(Dn.Value, Mch(n)) > 0 [COLOR=navy]Then[/COLOR]
                Dn.Offset(, n + 1) = Mch(n)
            [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] Dn
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick

Thanx Mick, but in brackets can be any words (symbols).
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Thanx Rick, I've used your updated macro:

...and it don't work as I showed in sample from the #5 .
Sorry, I missed that the last example value did not have parentheses around it. Give this modified macro a try instead...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitOutOfParentheses()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IFERROR(IF(A1:A#="""","""",SUBSTITUTE(MID(A1:A#,2,LEN(A1:A#)-2),MID(LEFT(A1:A#&""("",FIND(""("",A1:A#&""("",2)),FIND("")"",A1:A#),200),""("")),A1:A#)", "#", LastRow))
  Columns("B").TextToColumns , xlDelimited, , , False, False, False, False, True, "("
End Sub[/td]
[/tr]
[/table]
By the way, in the future when you get a response that does not work correctly, don't just say "it didn't work"... please tell us in what way it did not work (what happened that shouldn't have happened or, conversely, what did not happen that should have)... this will make it easier for us to track down where the problem is instead of our having to try and figure it out on our own.
 
Upvote 0
Sorry, I missed that the last example value did not have parentheses around it. Give this modified macro a try instead...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub SplitOutOfParentheses()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1:B" & LastRow) = Evaluate(Replace("IFERROR(IF(A1:A#="""","""",SUBSTITUTE(MID(A1:A#,2,LEN(A1:A#)-2),MID(LEFT(A1:A#&""("",FIND(""("",A1:A#&""("",2)),FIND("")"",A1:A#),200),""("")),A1:A#)", "#", LastRow))
  Columns("B").TextToColumns , xlDelimited, , , False, False, False, False, True, "("
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
By the way, in the future when you get a response that does not work correctly, don't just say "it didn't work"... please tell us in what way it did not work (what happened that shouldn't have happened or, conversely, what did not happen that should have)... this will make it easier for us to track down where the problem is instead of our having to try and figure it out on our own.

OK, Rick, I will try.

Results of the SplitOutOfParentheses:

ABCD
(yes)---(no)--(I don't know)--some text<strike></strike>yesno)--<strike></strike>I don't know)--some tex<strike></strike>
(yes)--(no)---some text<strike></strike>yes<strike></strike>no)---some tex<strike></strike><strike></strike>
some text<strike></strike>#REF!<strike></strike>

<tbody>
</tbody>

Example from the post #5 :

ABCD
1(yes)---(no)--(I don't know)--some textyesnoI don't know<strike></strike>
2(yes)--(no)---some textyesno-no-
3some text-no--no-<strike></strike>-no-

<tbody>
</tbody>
 
Last edited:
Upvote 0
Example from the post #5 :

ABCD
1(yes)---(no)--(I don't know)--some textyesnoI don't know<strike></strike>
2(yes)--(no)---some textyesno-no-
3some text-no--no-<strike></strike>-no-

<tbody>
</tbody>
Is what I highlighted in red actually what you want from the split out? If not, show me what you do want.
 
Upvote 0
There are can be <= 10 000 symbols in one cell and number of the text parts in the brackets <= 100.
There are can be any text in the brackets - not only "yes", "no", "I don't know".
There are can be any words around the brackets.
 
Upvote 0
Yes, Rick.
So you want -no- returned for text not in parentheses, correct?

If so, why in the last example where there is only one piece of text is -no- returned three times? What is the rule for how many times to repeat it?
 
Last edited:
Upvote 0
So you want -no- returned for text not in parentheses, correct?

If so, why in the last example where there is only one piece of text is -no- returned three times? What is the rule for how many times to repeat it?

This is the method to fill the empty cells in those columns that not empty.
 
Upvote 0
This is the method to fill the empty cells in those columns that not empty.
Okay, next question... is the only thing the can come between a closing parenthesis ()) and an opening parenthesis (() either nothing or two dashes like all your examples have shown so far? If not, then what can be there... any characters? If so, should they become -no- in the output like the text at the end in your examples?
 
Last edited:
Upvote 0
I wonder if there will be that cases, but if we find it I think we should grab only the text w/o the round brackets "(" text without the round brackets inside ")".

AB
(some text(another text))another text

<tbody>
</tbody>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,491
Messages
6,125,111
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top