Group Function

MikeDBMan

Well-known Member
Joined
Nov 10, 2010
Messages
608
In other languages I have worked with, I could use a group() function that would pick out a subset of text within a string. Group("1234 5678 9101112 13141516",2) ="5678" because 5678 is the second "grouping" of data in the string. Is there a similar function in VBA?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I don't believe there is one built into VBA, but I built a quick UDF that emulates it. If you need an optional spot to choose what delimiter to use (space, comma, semicolon, etc), lemme know:

Code:
Public Function GroupFind(iString As String, GrpNum As Long)
Dim i As Long
Dim tempstring As String
If GrpNum = 1 Then
   GroupFind = Left(iString, Application.Find(" ", iString))
Else
   tempstring = iString
   For i = 2 To GrpNum
      tempstring = Right(tempstring, Len(tempstring) - Application.Find(" ", tempstring))
   Next i
   If IsError(Application.Find(" ", tempstring)) Then
      GroupFind = tempstring
   Else
      GroupFind = Left(tempstring, Application.Find(" ", tempstring))
   End If
End If
End Function
Excel Workbook
A
11234 5678 9101112 13141516
29101112
Sheet1
Excel 2003
Cell Formulas
RangeFormula
A2=groupfind(A1,3)
 
Last edited:
Upvote 0
That is exactly what I wanted. I am not going to use it as a UDF but certainly could. I need it for coding in VBA. Thanks!
 
Upvote 0
You can use UDFs in VBA as well :biggrin:
 
Upvote 0
Also, I was able to work out a version where you can choose what delimiter to use. if you do not declare the delimiter, then it will default to a space:

Code:
Public Function GroupFind(iString As String, GrpNum As Long, Optional delim As Variant)
Dim i As Long
Dim tempstring As String
If IsMissing(delim) Then delim = Chr(32)
If GrpNum = 1 Then
   GroupFind = Left(iString, Application.Find(delim, iString) - 1)
Else
   tempstring = iString
   For i = 2 To GrpNum
      tempstring = Right(tempstring, Len(tempstring) - Application.Find(delim, tempstring))
   Next i
   If IsError(Application.Find(delim, tempstring)) Then
      GroupFind = tempstring
   Else
      GroupFind = Left(tempstring, Application.Find(delim, tempstring) - 1)
   End If
End If
End Function

Excel Workbook
A
11234 5678 9101112 13141516
29101112
3
41234/5678/9101112/13141516
59101112
6
71234;5678;9101112;13141516
89101112
Sheet1
Excel 2003
Cell Formulas
RangeFormula
A2=groupfind(A1,3)
A5=groupfind(A4,3,"/")
A8=groupfind(A7,3,";")



Note: This will only work for a delimiter that is 1 character. I'm working on figuring out how to handle it if the delimiter is more than 1 character.
 
Upvote 0
You are correct. It is still a UDF even when used only in Excel instead of "topside" in the worksheet itself. I am going to use your latest version with the optional delimiter which is extremely powerful. Thanks for creating it for me!
 
Upvote 0
GOT IT! And glad it works for you. I love taking on fun projects like this... always ends up giving me ideas for future projects (or at least it teaches me something along the way!)

Code:
Public Function GroupFind(iString As String, GrpNum As Long, Optional delim As Variant)
Dim i As Long
Dim tempstring As String
If IsMissing(delim) Then delim = Chr(32)
If GrpNum = 1 Then
   GroupFind = Left(iString, Application.Find(delim, iString) - 1)
Else
   tempstring = iString
   For i = 2 To GrpNum
      tempstring = Right(tempstring, Len(tempstring) - Application.Find(delim, tempstring))
   Next i
   If IsError(Application.Find(delim, tempstring)) Then
      GroupFind = Right(tempstring, Len(tempstring) - Len(delim) + 1)
   Else
      GroupFind = Left(Right(tempstring, Len(tempstring) - Len(delim) + 1), Application.Find(delim, tempstring) - Len(delim))
   End If
End If
End Function

Excel Workbook
A
71234;5678;9101112;13141516
89101112
9
101234DELIMTHIS5678DELIMTHIS9101112DELIMTHIS13141516
1113141516
Sheet1
Excel 2003
Cell Formulas
RangeFormula
A8=groupfind(A7,3,";")
A11=groupfind(A10,4,"DELIMTHIS")
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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