Question : VBA using Search Worksheet Function to Find Question Marks (Tilde)

I need a function which checks a string (derived from a range containing game scores like "10,8" or "?,?") to see if they contain a "?".  If they do then the game has not yet been played.

I have tried using WorksheetFunction.Search but have had no success with it when I include a ? in the search text.  When I prefix  the ? character with the ~ tilde character, it fails with Run Time Error 1004 - Macro Error.
Searching for "," or"11", works fine.

I do not really want to output the scores to a test cell "A1" but took this appraoch to help debug the code.

BTW, I need the function to return FALSE if a ? is found, otherwise TRUE.

I am really at first base with VBA so please explain!

Cheers

Martyn
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
Public Function Played(player, team1, team2)
   
    Dim state As Variant
    Dim ref As String
    Dim ref1 As String
    Dim ref2 As String
       
    ref1 = "A1"
    '   test cell
    Set myRange = Worksheets("Match Results").Range(ref1)
    ref2 = GetScores(player, team1, team2)
    '   function returns the scores "10,11", or "?,?" etc
    myRange.Value = ref2
    state = Application.WorksheetFunction.Search("~?", myRange.Value)
    '   state = Application.Evaluate("Search(10, ref)")
    If state = "#VALUE!" Then
        Played = False
    Else
        Played = True
    End If
 
End Function

Answer : VBA using Search Worksheet Function to Find Question Marks (Tilde)

Some suggestions

- you need to either make the function volatile (as I have done below), or use the function in conjunction with a volatile formula like RAND() to force it to update
- declare your Function as Boolean
- declare all the variables
- suggest you run the GetScores function in the same function given you pull the variables in here
- Boolean is False by defulat so you can remove the 'state' logic

Cheers

dave
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
Public Function Played(Player As String, team1 As String, team2 As String) As Boolean
    Application.Volatile True
    Dim ref As String
    Dim ref1 As String
    Dim ref2 As Range, myRange As Range
 
    'ref1 = "A1"
    '   test cell
    'Set myrange = Worksheets("Match Results").Range(ref1)
    'ref2 = GetScores(Player, team1, team2)
    '   function returns the scores "10,11", or "?,?" etc
    'myrange.Value = ref2
 
    'my test
    Set myRange = Range("a2")
    If InStr(myRange.Value, "?") > 0 Then Played = True
 
End Function
Random Solutions  
 
programming4us programming4us