Two numbers to left of * and two numbers to right of *

spencer_time

Board Regular
Joined
Sep 19, 2019
Messages
55
Office Version
  1. 365
  2. 2016
  3. 2007
Platform
  1. Windows
Hello,

I -had- part of my program working until I realized that the length of the filename wasn't always constant and I need help fixing that.
In the following section of code it used to look for a sheet names *CEQ* and extract the two numbers before CEQ and the two numbers after CEQ. I then started trying to make it more universal to work in my final application and swapped CEQ for the variable "response" (which gets its value from a user form and can be 1-6 characters, all letters) but then my MID functions to extract the 2 (maybe occasionally 3) numbers before and after the response variable will no longer work.

Code:
    If curSheet.Name Like "*" & response & "*" Then
        partNum = Mid(curSheet.Name, 1, 2) 'extract part number from filename to use in specifying where to save other information into array
        dataSet(3, 0, partNum, designator) = Mid(curSheet.Name, 6) 'extract dB value from filename and save to dataset array
        dataSet(9, 0, partNum, designator) = Mid(curSheet.Name, 1, 2) 'extract part number from filename and save to dataset array

Is there a way to make the program not care if the sheet name is 105CEQ33 or 08CEQ4, and always extract the number before and the number after the the letters and save them into separate variables?

Thanks in advance.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this

Code:
Sub test()
  Dim response As String, part1 As String, part2 As String
  Dim curSheet As Worksheet
  Set curSheet = ActiveSheet
  response = "CEQ"
  If curSheet.Name Like "*" & response & "*" Then
    part1 = Left(curSheet.Name, InStr(1, curSheet.Name, response) - 1)
    part2 = Mid(curSheet.Name, InStr(1, curSheet.Name, response) + Len(response))
  End If
End Sub
 
Upvote 0
Solution
Another way to consider...
Code:
Sub test()
  Dim Response As String, Parts() As String
  Response = "CEQ"
  Parts = Split(ActiveSheet.Name, Response)
  If UBound(Parts) Then
[B][COLOR="#008000"]    ' Parts(0) will contain the number before the Response
    ' Parts(1) will contain the number after the Response[/COLOR][/B]
  End If
End Sub
 
Upvote 0
Thanks for your help guys, both of these appear to work. I'm going to go with DanteAmor's suggestion as I understand it better and can troubleshoot it if something goes wrong, but I will save your suggestion too Rick Rothstein, and once I'm done with my big spreadsheet will play around with it and see if it might can be used to make my spreadsheet any better.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,189
Members
452,616
Latest member
intern444

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