-
VBA - Case Selects
Hi, I'm having a problem with a macro, trying to change it in this way:
Code:
Sub Main()
Dim strings As String()
Dim i As Integer
Dim quotes As Char = ChrW(34)
strings = New String() {"started", "starting", _
"ended", "ending"}
Console.WriteLine(" test every string to see if it starts with 'st'")
For i = 0 To strings.GetUpperBound(0)
If strings(i).StartsWith("st") Then
Console.WriteLine(quotes & strings(i) & quotes & _
" starts with " & quotes & "st" & quotes )
End If
Next
Console.WriteLine(" test every string to see if it ends with 'ed'")
For i = 0 To strings.GetUpperBound(0)
If strings(i).EndsWith("ed") Then
Console.WriteLine( quotes & strings(i) & quotes & _
" ends with " & quotes & "ed" & quotes )
End If
Next
End Sub ' Main
I'm trying to use Case statements, but I've tried a lot and can't seem to get it to work using that... how would I use Select Case and Case?
Thanks!
-
Re: VBA - Case Selects
I don't see any Select Case in your code. What are you trying to do exactly?
Also, you missed the forum. I'll notify Mods to move it to the VBA section (which is different than VB Classic).
EDIT: This is how select case works: http://www.vbtutor.net/lesson8.html
-
Re: VBA - Case Selects
Sorry, I can't seem to find the VBA forum....
Anyhow, I want to go from the above, to the following, but it just skips:
Code:
Sub Main()
Dim strings As String()
Dim i As Integer
Dim quotes As Char = ChrW(34)
strings = New String() {"started", "starting", _
"ended", "ending"}
Console.WriteLine(" test every string to see if it starts with 'st'")
For i = 0 To strings.GetUpperBound(0)
Case Select testcase
Case strings(i).EndsWith("ed") Then
MsgBox(quotes & strings(i) & quotes & _
" Ends With " & quotes & "st" & quotes )
End If
Next
End Sub ' Main
-
Re: VBA - Case Selects
Where is "testcase" declared and how? And if you are checking it in a loop, then you need to set it in the loop as well to make any sense. You also lack the End Select statement. There's no "Then" keyword either. Take a look at the link I posted in the previous post to see how to use it.
P.S: VBA is under Office Development.
-
Re: VBA - Case Selects
I read that tutorial, and tried to apply it to the macro, came out with this:
Code:
Sub Main()
Dim strings As String()
Dim i As Integer
Dim quotes As Char = ChrW(34)
Dim testcase As String
strings = New String() {"started", "starting", _
"ended", "ending"}
Console.WriteLine(" test every string to see if it starts with 'st'")
For i = 0 To strings.GetUpperBound(0)
Select testcase
Case strings(i).EndsWith("ed")
MsgBox(quotes & strings(i) & quotes & _
" Ends With " & quotes & "ed" & quotes)
Case strings(i).EndsWith("ing")
MsgBox(quotes & strings(i) & quotes & _
" Ends With " & quotes & "ing" & quotes)
Case Else
MsgBox("Does not end with parameters")
End Select
Next
End Sub ' Main
Obviously, it's going to fail... what does testcase need to be declared as(I have a string)? Does it need to be filled with something?
I appreciate the help.... I'm more of a C# guy :)
-
Re: VBA - Case Selects
The testcase should be whatever you are checking against. You need to set it's value somewhere. As it is now, it si just an empty string. But I don't think it's the right construct for your scenario. You use it usually as a replacement for If Then, when there are a lot of values (cases) to check.
In your case I'd stick with the code you had in the first place. Why did you think you need to replace it with Select Case?
-
Re: VBA - Case Selects
I wanted to use case because I have a possibility of three or four different endings, (y, ss, s, and nothing) and am going to trim and make them plural. The case statement was a recommendation of a co-worker
-
Re: VBA - Case Selects
I'm still don't think that it would be aplicable to your situation. The only change I'd make to your original code is to pack everything in one For loop so you go through the items only once, not twice (or more if you have more cases to test).
Code:
Sub Main()
Dim strings As String()
Dim i As Integer
Dim quotes As Char = ChrW(34)
strings = New String() {"started", "starting", _
"ended", "ending"}
For i = 0 To strings.GetUpperBound(0)
Console.WriteLine(" test every string to see if it starts with 'st'")
If strings(i).StartsWith("st") Then
Console.WriteLine(quotes & strings(i) & quotes & _
" starts with " & quotes & "st" & quotes )
End If
Console.WriteLine(" test every string to see if it ends with 'ed'")
If strings(i).EndsWith("ed") Then
Console.WriteLine( quotes & strings(i) & quotes & _
" ends with " & quotes & "ed" & quotes )
End If
Next
End Sub ' Main
-
Re: VBA - Case Selects
Thread moved to 'Office Development/VBA' forum.
(thanks for letting us know baja_yu :thumb: )
-
Re: VBA - Case Selects
Seems like you wanted the VB.NET Forum and not the VBA Forum ;)
I will inform the mods and they will move it to VB.NET if they find it appropriate...
-
Re: VBA - Case Selects
I see what you mean koolsid, there are a few lines of code that look like .Net
brstorrie, which version of VB are you actually using for this?