Solved

Check a list of string in a input parameter

Posted on 2015-02-03
6
82 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 50

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 50

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

685 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