Hi,
I have a situation where i an array of strings. I am supposed to allow the user to select one of three options
ABC*
*ABC
*ABC*
Any suggestions on how to program it so i account for all 3 or do i have to have multiple if statements?
Hi,
I have a situation where i an array of strings. I am supposed to allow the user to select one of three options
ABC*
*ABC
*ABC*
Any suggestions on how to program it so i account for all 3 or do i have to have multiple if statements?
Do all options have the same outcome?
If posting code please use code tags, see here.
In VBA, you might use:
where "s" is a String or Variant variable. Of course, you might use s(i) for a single string element in a one-dimensional array. (Insufficient information for us to provide a turn-key answer.)![]()
If InStr(s,"ABC") > 0 Then ' ABC* or *ABC or *ABC* End If
But beware: that only recognizes all-uppercase instances of "ABC". If you want to allow for lowercase as well as uppercase, try:
If InStr(UCase(s),"ABC") > 0 Then
Maybe something like this...
![]()
Dim i As Integer, strABC As String strABC = "ABC" i = Application.InputBox("Select search type: " & vbLf & _ vbTab & "1.) Starts with " & strABC & vbLf & _ vbTab & "2.) Ends with " & strABC & vbLf & _ vbTab & "3.) Contains " & strABC & vbLf, _ "Search Type", Type:=1) Select Case i Case 1 strABC = strABC & "*" Case 2 strABC = "*" & strABC Case 3 strABC = "*" & strABC & "*" Case Else Exit Sub End Select
Surround your VBA code with CODE tags e.g.;
[CODE]your VBA code here[/CODE]
The # button in the forum editor will apply CODE tags around your selected text.
i responded yesterday but i guess i got disconnected and it never got posted.
So here is the situation.
Sometimes the user wants to
1-Have starts with "ABC"
2-Ends with "ABC"
3-Contains "ABC"
Now depending on which it is i will analyze the string for this scenario. The only way i know to do it now is to have 3 if/then statements one if its starts with , one if its ends with and one if its contains.......
was wondering if there was a slick way to only have a single if/then
any ideas?
Use this syntax for IF
If Then
ElseIf Then
ElseIf Then
End If
Or use the Select Case statement as in the example code above which does the same thing.
Last edited by AlphaFrog; 05-19-2014 at 06:21 PM.
ok......i dont think i explained this well but i think i found the answer.
so lets suppose that the user can put one of the following in cell A1: "Site*", "*Site", "*Site*"
Let the value of cell A1 equal the variable string1
now lets suppose in cell B1 i put "abcSite"
Let the value of cell B1 equal the variable string2
now i want to compare string2 (cell B1 contents) to cell A1
i can do the following
![]()
if string2 like string2 then msgbox("do something" endif
i dont need to do 3 if/then comparisons.......just one.
make sense?
anyone see any issues with this method? better to ask and find out now vs program it and find out later.......
thanks again for all your help and sorry for the confusion
It should be fine - just remember that Like is also case sensitive so if you don't want that, you shoudl use LCase$ (or UCase$) on both variables.
Everyone who confuses correlation and causation ends up dead.
thanks.....I did not know that about "Like" so I am glad I asked......
thanks to all who helped......I appreciate it.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks