Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check a list of string in a input parameter

Posted on 2015-02-03
6
Medium Priority
?
92 Views
Last Modified: 2015-02-03
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
Comment
Question by:AXISHK
  • 2
  • 2
  • 2
6 Comments
 
LVL 53

Assisted Solution

by:Rgonzo1971
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

Open in new window

Regards
0
 
LVL 31

Expert Comment

by:gowflow
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

Open in new window


gowflow
0
 
LVL 37

Expert Comment

by:Neil Russell
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 53

Assisted Solution

by:Rgonzo1971
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

Open in new window

0
 
LVL 31

Accepted Solution

by:
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

Open in new window


gowflow
0
 
LVL 37

Expert Comment

by:Neil Russell
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question