VBA to grab each square bracketed string in a sentence

tdai

New Member
Joined
Sep 9, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Assuming my sentences contain unknown number of square bracketed strings, like:
1. This is [dog], that is [monkey] and i have also a [goose]..
2. [one] is good but i want to have [two hundred] cars
How can I fetch only square bracketed strings using VBA code? For example:
for the first sentence i want it to return: [dog] [monkey] [goose]
for the 2nd sentence i want it to return: [one] [two hundred]

I am on MAC, so Regex is not an option unfortunately. Thank you!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
here's one way.

Book1
AB
11. This is [dog], that is [monkey] and i have also a [goose]..[dog] [monkey] [goose]
22. [one] is good but i want to have [two hundred] cars[one] [two hundred]
Sheet4
Cell Formulas
RangeFormula
B1:B2B1=Brackets(A1)


VBA Code:
Function Brackets(s As String)
Dim b As Boolean:   b = False
Dim m As String
Dim tmp As String
Dim res As String

For i = 1 To Len(s)
    m = Mid(s, i, 1)
    If m = "[" Then b = True
    If b Then tmp = tmp & m
    If m = "]" Then
        b = False
        res = res & tmp & " "
        tmp = vbNullString
    End If
Next i

Brackets = Trim(res)
End Function
 
Upvote 0
Another way using Power Query if you have that available.

Book1
ABC
1Column1Count
21. This is [dog], that is [monkey] and i have also a [goose]..[dog] [monkey] [goose]
32. [one] is good but i want to have [two hundred] cars[one] [two hundred]
Sheet3


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Index = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
    Split = Table.ExpandListColumn(Table.TransformColumns(Index, {{"Column1", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1"),
    TextBefore = Table.AddColumn(Split, "Text Before Delimiter", each Text.BeforeDelimiter([Column1], "]"), type text),
    SR = Table.SelectRows(TextBefore, each Text.Contains(_[Column1],"]")),
    Group = Table.Group(SR, {"Index"}, {{"Count", each _, type table [Column1=nullable text, Index=number, Text Before Delimiter=text]}}),
    Combine = Table.TransformColumns(Group,{{"Count", each Text.Combine(List.Transform(_[Text Before Delimiter], each "[" & _ & "]")," ")}}),
    RC = Table.RemoveColumns(Combine,{"Index"})
in
    RC
 
Upvote 0
I threw another way together:

Code:
Function betwB(text As String)
Dim firstB As Long, secondB As Long, stringBtw As String, i As Long, lr As Long
lr = Cells(Rows.Count, "A").End(xlUp).Row
betwB = ""
While 0 <> InStr(text, "[")
firstB = InStr(text, "[")
secondB = InStr(text, "]")
betwB = betwB & " " & Mid(text, firstB, secondB - firstB + 1)
text = Right(text, Len(text) - secondB)
Wend
End Function
 
Upvote 0
I believe this one-line function will also work...
VBA Code:
Function Brackets(ByVal S As String) As String
  Brackets = IIf(S Like "*[*]*", "[", "") & Replace(Join(Filter(Split(Replace(S, "]", "|["), "["), "|"), "["), "|", "]")
End Function
 
Last edited:
Upvote 0
Solution
wow - i'm just amazed by so many creative ways of getting this done! Thank you everyone for the brilliant ideas!!!

I will use Rick's one -line function in my code.
 
Upvote 0

Forum statistics

Threads
1,215,684
Messages
6,126,199
Members
449,298
Latest member
Jest

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