|
-
Sep 27th, 2010, 08:23 PM
#1
Thread Starter
New Member
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!
-
Sep 27th, 2010, 08:28 PM
#2
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
-
Sep 27th, 2010, 08:33 PM
#3
Thread Starter
New Member
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
-
Sep 27th, 2010, 08:37 PM
#4
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.
-
Sep 27th, 2010, 08:47 PM
#5
Thread Starter
New Member
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
-
Sep 27th, 2010, 08:56 PM
#6
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?
-
Sep 27th, 2010, 08:58 PM
#7
Thread Starter
New Member
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
-
Sep 27th, 2010, 09:03 PM
#8
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
-
Sep 28th, 2010, 05:37 AM
#9
Re: VBA - Case Selects
Thread moved to 'Office Development/VBA' forum.
(thanks for letting us know baja_yu )
-
Sep 29th, 2010, 01:30 AM
#10
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Sep 29th, 2010, 05:45 AM
#11
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|