VBA - Check if string contain value

sparda663

New Member
Joined
Apr 30, 2015
Messages
7
Hi everyone, i have an issue :

I'm using an application which need to check a filename that i store in a string like :

strName = file.Name

For example, the file name is : "Self Assessment Referential - C+ : 55% "

I need to get the C+ in a string (gradeLetter) and 55 in an integer (gradeNumber)

How is it possible?

Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
68,059
Office Version
  1. 365
Platform
  1. Windows
It is heavily recommended that you avoid use special characters like "+", ":", or "%" in file names. That could present problems.
It is generally recommended that file names only contains letters, numbers, and underscores (it is even recommended that you not use spaces, though those aren't quite as problematic as the other characters).
 
Upvote 0

vaskov17

Well-known Member
Joined
Apr 27, 2011
Messages
921
If your file format stays the same, try the code below. It assumes the grade letter is always between "-" and ":" and it assumes the grade number is between ":" and "%", also that there are no other "-", ":" and "%" in the file name. Not a good idea to have these characters in a file name but if you do this code should help you.

Code:
Sub macro()
    Dim strFile As String
    Dim gradeLetter As String
    Dim gradeNumber As Integer
    
    'insert code to get file name into strFile here
    
    gradeLetter = Replace(Mid(strFile, InStr(1, strFile, "-", vbTextCompare) + 1, InStr(1, strFile, ":", vbTextCompare) - InStr(1, strFile, "-", vbTextCompare) - 1), " ", "")
    gradeNumber = Val(Mid(strFile, InStr(1, strFile, ":", vbTextCompare) + 1, InStr(1, strFile, "%", vbTextCompare) - InStr(1, strFile, ":", vbTextCompare) - 1))
    MsgBox "Grade letter: " & gradeLetter & vbCrLf & "Grade number: " & gradeNumber
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,746
Messages
6,011,416
Members
441,613
Latest member
worksux

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
Top