Solved

Check a list of string in a input parameter

Posted on 2015-02-03
6
78 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 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 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 29

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 49

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 200 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 29

Accepted Solution

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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