Results 1 to 11 of 11

Thread: VBA - Case Selects

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    10

    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!

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    10

    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

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    10

    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

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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?

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    10

    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

  8. #8
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    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

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: VBA - Case Selects

    Thread moved to 'Office Development/VBA' forum.

    (thanks for letting us know baja_yu )

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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
  •  



Click Here to Expand Forum to Full Width