Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Check a list of string in a input parameter

Posted on 2015-02-03
Medium Priority
90 Views
Is there a smarter way to code the following ? Ts

Private Function IsPrice(stCode As String) As Boolean

On Error Resume Next

If InStr(stCode, "9K") > 0 Then
IsPrice = True
Exit Function
ElseIf InStr(stCode, "14K") > 0 Then
IsPrice = True
Exit Function
ElseIf InStr(stCode, "16K") > 0 Then
IsPrice = True
Exit Function
ElseIf InStr(stCode, "18K") > 0 Then
IsPrice = True
Exit Function
ElseIf InStr(stCode, "10K") > 0 Then
IsPrice = True
Exit Function
End If
0
Question by:AXISHK
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2

LVL 52

Assisted Solution

Rgonzo1971 earned 800 total points
ID: 40585720
HI,

You could try

``````Private Function IsPrice(stCode As String) As Boolean

If stCode Like "*9K*" Or stCode Like "*14K*" Or _
stCode Like "*16K*" Or stCode Like "*18K*" Or stCode Like "*10K*" Then
IsPrice = True
Else
IsPrice = False
End If
End Function

Private Function IsPrice(stCode As String) As Boolean

IsPrice = (stCode Like "*9K*" Or stCode Like "*14K*" Or _
stCode Like "*16K*" Or stCode Like "*18K*" Or stCode Like "*10K*")
End Function
``````
Regards
0

LVL 31

Expert Comment

ID: 40585780
Try this

``````Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String

sPrice = "9K,14K,16K,18K,10K"
If InStr(1, stCode, sPrice) <> 0 Then
IsPrice = True
Else
IsPrice = False
End If

End Function
``````

gowflow
0

LVL 37

Expert Comment

ID: 40585793
bear in mind that depending on what one of the above 3 you choose, you can get TRUE returned for:-

9K
9K,14
16Kilo
118Kettles

Is this what you wanted?
0

LVL 52

Assisted Solution

Rgonzo1971 earned 800 total points
ID: 40585799
@gowflow

your code will only work if you swap sPrice and stCode and only if the string test consists only of 9K or 14K, etc.
``````Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String

sPrice = "9K,14K,16K,18K,10K"
If InStr(1, sPrice, stCode) <> 0 Then
IsPrice = True
Else
IsPrice = False
End If

End Function
``````
0

LVL 31

Accepted Solution

gowflow earned 1200 total points
ID: 40585901
Sorry I missed something it should be this

``````Private Function IsPrice(stCode As String) As Boolean
Dim sPrice As String
Dim vPrice
Dim I As Long

sPrice = "9K,14K,16K,18K,10K"
vPrice = Split(sPrice, ",")

For I = LBound(vPrice) To UBound(vPrice)
If InStr(1, stCode, vPrice(I)) <> 0 Then
IsPrice = True
Exit For
End If
Next I

End Function
``````

gowflow
0

LVL 37

Expert Comment

ID: 40586083
As the questioner does not explain EXACTLY what the contents of stCode could be at the point of calling the function then all anybody can do is make a guess at what is wanted.

IF the only contents would be an exact strink of "9K"  or "14K" or "16K" or "10K" etc then none of the above are optimal.  What happens when your stock codes change or you add a new one?

If indeed you only have and only ever will have those 5 stock codes then something like the above would work.

However if your stock is fluid and liable to change the I would suggest that you read the available stock codes into an array from the database and validate against those each time.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
###### Suggested Courses
Course of the Month9 days, 18 hours left to enroll