VBA Code for MS Excel Formula to Extract before and after specified character

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance.

I have a MS Excel formula that I would like to convert to VBA. For simplicity I have shortened the code. Yes I know I could just input B2 into the following formula instead of EntryVal1, but as I indicated, I shortened the code so, please understand. What I'm trying to do is extract a value after a character and before another character.

So for example, if B2 is as follows:
Bears vs. Doe, John Foster (SPC: 586982) (DOP: 04/05/1991)

I want C2 to be:
Doe, John Foster

normally, I would just use the following formula into C2, but I am doing something different "=MID(LEFT(A2, FIND(" (SPC: ",A2) -1), FIND("vs. ", A2) + 4, LEN(A2) )"

I am having trouble with the following line
VBA Code:
EntryVal2 = "=MID(LEFT(" & """ & EntryVal1 & """ & ", FIND("" (SPC: ""," & """ & EntryVal1 & """ & ") -1), FIND(""vs. ""," & """ & EntryVal1 & """ & ") + 4, LEN(" & """ & EntryVal1 & """ & ") )"

Where my code is as follows

VBA Code:
Sub ValGet()

Dim i as Long
Dim EntryVal1 as String
Dim EntryVal2 as String

For i = 2 to 10

EntryVal1 = Range("B" & i).Value
EntryVal2 = "=MID(LEFT(" & """ & EntryVal1 & """ & ", FIND("" (SPC: ""," & """ & EntryVal1 & """ & ") -1), FIND(""vs. ""," & """ & EntryVal1 & """ & ") + 4, LEN(" & """ & EntryVal1 & """ & ") )"

Range("C" & i).Formula = EntryVal2
Range("C" & i).Formula = Range("C" & i).Value

Next i


End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try
VBA Code:
EntryVal2 = "=MID(LEFT(" &  EntryVal1  & ", FIND("" (SPC: ""," &  EntryVal1  & ") -1), FIND(""vs. "","  & EntryVal1  & ") + 4, LEN("  & EntryVal1  & ") )"
 
Upvote 0
Another option, without the loop
VBA Code:
With Range("C2:C10")
   .Value = Evaluate(Replace("MID(LEFT(@, FIND("" (SPC: "",@) -1), FIND(""vs. "",@) + 4, LEN(@) )", "@", .Offset(, -1).Address))
End With
 
Upvote 0
Try
VBA Code:
EntryVal2 = "=MID(LEFT(" &  EntryVal1  & ", FIND("" (SPC: ""," &  EntryVal1  & ") -1), FIND(""vs. "","  & EntryVal1  & ") + 4, LEN("  & EntryVal1  & ") )"
Thanks so much @Fluff for the quick response. When I update with that code, I get the following error: "Runtime Error 1004 "Application-defined or Object-defined error" on the line:
VBA Code:
Range("C" & i).Formula = EntryVal2
 
Upvote 0
In that case try
VBA Code:
EntryVal2 = "=MID(LEFT(""" & EntryVal1 & """, FIND("" (SPC: "",""" & EntryVal1 & """) -1), FIND(""vs. "",""" & EntryVal1 & """) + 4, LEN(""" & EntryVal1 & """) )"
 
Upvote 0
Solution
In that case try
VBA Code:
EntryVal2 = "=MID(LEFT(""" & EntryVal1 & """, FIND("" (SPC: "",""" & EntryVal1 & """) -1), FIND(""vs. "",""" & EntryVal1 & """) + 4, LEN(""" & EntryVal1 & """) )"
Thanks. That worked perfectly.
 
Upvote 0
To extract a substring from A2:A10, just use VBA mid function:

VBA Code:
Sub test()
Dim cell As Range
For Each cell In Range("A2:A10")
If cell <> "" Then
cell.Offset(, 1).Value = Trim(Mid(cell, InStr(cell, "vs.") + 4, InStr(cell, "(SPC:") - InStr(cell, "vs.") - 4))
End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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