Using VBA to extract data between mismatched delimiters

NoleJenny

New Member
Joined
Aug 6, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'm relatively inexperienced with VBA. I'm trying to use it to extract size and quantity data from a string. The size is found after the second ":" and before the first ")". Here's an example of one of the text strings, but I'll be doing this for an entire column of data.

First Unitarian Church of Dallas T-shirt (Amount: 10.00 USD, Quantity: 2, Size: Adult 2XL)
Total: $20.00

Here's what I have so far, but I'm getting a "subscript out of range" error on the italicized row.

Rich (BB code):
Sub ExtractSizesAndQuantities()

        Dim lastRow As Long
            lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
        Dim Size As String
        Dim i As Integer
        Dim Text As String

        For i = 2 To lastRow
            Text = Sheets("Orders").Range("D" & i)
            Size = Split(Text, ":")(2)
            Sheets("Orders").Range("F" & i) = Size
            Next i

End Sub

This is my first time posting, so let me know if I've left anything out. Thanks!
 
Last edited by a moderator:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Welcome to the Board!

Do you need VBA at all? Maybe you can use the new TEXTBEFORE, TEXTAFTER, and TEXTSPLIT functions in Excel, i.e.
1722977579005.png


Formula in cell B2:
Excel Formula:
=TEXTBEFORE(TEXTAFTER(A2,"Quantity: "),",")

Formula in cell C2:
Excel Formula:
=TEXTBEFORE(TEXTAFTER(A2,"Size: "),")")
 
Upvote 0
Solution
I'm relatively inexperienced with VBA. I'm trying to use it to extract size and quantity data from a string. The size is found after the second ":" and before the first ")". Here's an example of one of the text strings, but I'll be doing this for an entire column of data.

First Unitarian Church of Dallas T-shirt (Amount: 10.00 USD, Quantity: 2, Size: Adult 2XL)
Total: $20.00

Here's what I have so far, but I'm getting a "subscript out of range" error on the italicized row.

Sub ExtractSizesAndQuantities()

Dim lastRow As Long
lastRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Dim Size As String
Dim i As Integer
Dim Text As String

For i = 2 To lastRow
Text = Sheets("Orders").Range("D" & i)
Size = Split(Text, ":")(2)
Sheets("Orders").Range("F" & i) = Size
Next i

End Sub

This is my first time posting, so let me know if I've left anything out. Thanks!

Welcome to the Board!

Do you need VBA at all? Maybe you can use the new TEXTBEFORE, TEXTAFTER, and TEXTSPLIT functions in Excel, i.e.
View attachment 115056

Formula in cell B2:
Excel Formula:
=TEXTBEFORE(TEXTAFTER(A2,"Quantity: "),",")

Formula in cell C2:
Excel Formula:
=TEXTBEFORE(TEXTAFTER(A2,"Size: "),")")
That works! I guess I was overcomplicating it lol. Thank you!
 
Upvote 0
You are welcome!
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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