+ Reply to Thread
Results 1 to 15 of 15

Select Case test String is failing.

Hybrid View

  1. #1
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Select Case test String is failing.

    Hi,

    I am trying to test some text that is entered via a textBox in a userForm through a Case Statement.

    Its not working.
    Hers what Im doing:
    Dim uiValue As String
        uiValue = uF_Helper.TextBox1.Text
    
    uiValue = Replace(uiValue, ", ", """, """)
    
    Dim wS As Worksheet
        
        For Each wS In Worksheets
            Select Case wS.Name
                Case "Sheet1", "Sheet2", "Sheet3"
                         'Do Nothing
                Case uiValue
                Stop
            Case Else
                         'Do Something
    In the textBox I enter some text:
    Dog, Cat, Snake

    Using Replace I am inserting the Quotes it should have, but its not hitting the Stop.
    What am I doing wrong?

    TIA for your help.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Select Case test String is failing.

    In your code you just matching the Worksheet Names with your case conditions.

    So your worksheet name and case condition both should match to run the given task.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    Hi,

    Thanks.

    Maybe Im not following what youre saying. It sounds like what youre telling me is that the test need to be of like variables/items
    So here is what I tried:
    Dim TestString      As String
        For Each wS In Worksheets
         TestString = wS.Name
            Select Case TestString
    That didnt work, so maybe Im not following what youre saying or I didnt do it right.
    Could you throw me another bone on this?
    TIA

  4. #4
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    This is what it looks like in the Locals window
    Expression________Value__________Type
    uiValue : "CAT", "DOG", "GOAT" : String

  5. #5
    Valued Forum Contributor
    Join Date
    11-26-2012
    Location
    Sydney
    MS-Off Ver
    2010
    Posts
    422

    Re: Select Case test String is failing.

    As far as VBA is concerned, uiValue is a single string - not three strings separated by commas. I see what you are trying to do, but I don't think there is a way to make that work.

    This might work for you:
      Dim x as Long
      Dim uiValue() As String
      Dim wS As Worksheet
      
      uiValue = Split(uF_Helper.TextBox1.Text, ",")
    
      For Each wS In Worksheets
        For x = 0 To UBound(uiValue)
          Select Case wS.Name
            Case "Sheet1", "Sheet2", "Sheet3"
              'Do Nothing
            Case Trim(uiValue(x))
              Stop
            Case Else
              'Do Something
          End Select
        Next
      Next
    Last edited by mgs73; 12-12-2015 at 05:27 AM.

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: Select Case test String is failing.

    Try
        uiValue = uF_Helper.TextBox1.Txt
        For Each wS In Worksheets
            Select Case True
                Case wS.Name = "Sheet1", wS.Name = "Sheet2", wS.Name = "Sheet3"
                Case InStr("," & uiValue & ",", "," & wS.Name & ",")
                    MsgBox wS.Name
                Case Else
                    ' do somwthing else
            End Select
        Next

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Select Case test String is failing.

    Hi dlow,

    What we are dealing with is a feature of Excel 'Case Statements'. Branches of 'Case Statements' should be constants and not evaluations, since Excel VBA apparently compiles the 'Case' branches as constants.

    The 'Select Case' construction should probably be replaced with a 'Compound If' construction.

    See the following example macros that are included in the attached file.

    Option Explicit
    
    Sub SimplifiedDemonstrationOfFaultyCaseConstruction()
      'This fails because the 'Instr() Case' is doing an evaluation
      'It looks like it should work, but apparently Excel VBA compiles the construction to a constant
      '
      'Make 'SheetX' the Active Sheet
    
      Dim uiValue As String
      
      uiValue = "SheetX"
      
      Sheets("SheetX").Activate
      
      Select Case ActiveSheet.Name
      
        Case "Sheet1", "Sheet2", "Sheet3"
          'Do Nothing
          MsgBox "Sheet1, Sheet2, Sheet3"
          
        Case InStr("," & uiValue & ",", "," & ActiveSheet.Name & ",")
          MsgBox "SheetX success: uivalue=" & uiValue
          
        Case Else
          MsgBox "Case Failure - 'SheetX' branch should have been selected"
    
      End Select
    
    End Sub
    
    Sub ReplaceCaseStatementWithCompoundIf()
    
      Dim uiValue As String
      
      uiValue = "SheetX"
      
      Sheets("SheetX").Activate
      
      If ActiveSheet.Name = "Sheet1" Or ActiveSheet.Name = "Sheet2" Or ActiveSheet.Name = "Sheet3" Then
        'Do Nothing
        MsgBox "Sheet1, Sheet2, Sheet3"
          
      ElseIf InStr("," & uiValue & ",", "," & ActiveSheet.Name & ",") Then
        MsgBox "SheetX success: uivalue=" & uiValue
          
      Else
        MsgBox "if Failure - 'SheetX' branch should have been selected"
    
      End If
    
    End Sub
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Option Explicit
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    10-05-2014
    Location
    Regina, SK Canada
    MS-Off Ver
    Excel for Mac 11
    Posts
    330

    Re: Select Case test String is failing.

    Just a note about SELECT & CASE

    You can test a number of logical decisions if there isn't any variable at the start, just 'True"
    The first case decision that matches "True" will be the one that is acted on.

    Here's a simple example….
    Sub Animal_Sound()
      Dim animal As String
      Dim snd As String
      
      animal = UCase(Range("C5").Value)
      
      Select Case True
        Case animal = "DOG"
          snd = "Bow Wow"
        Case animal = "CAT"
          snd = "Meow"
        Case animal = "COW"
          snd = "Moo-sic"
        Case Else
          snd = "???"
      End Select
      
      Range("C6") = snd
      
    End Sub

    So I suspect something similar would work in your case. (ha ha, no pun intended.)
    If this has been helpful, please click on the star at the left.

  9. #9
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    Hi,

    Wow thanks for the outpouring of help gang, I appreciate you.

    Yes mgs73 I think you are correct & from what our friend LJMetzger shared its confirmed. Thanks Lewis for sharing the info about the Branches needing to be constants.

    So, what I ended up doing was just use the VBIDE to write the line of code & at the end remove/clean up that line.
    It works great!
    Heres what that looks like:
    
    Sub AddCaseSta() 'http://www.cpearson.com/excel/vbe.aspx
    
    Dim VBProj      As VBIDE.VBProject
    Dim VBComp      As VBIDE.VBComponent
    Dim CodeMod     As VBIDE.CodeModule
    Dim LineNum     As Long
    
    Dim uiValue    As String
    
        uiValue = uF_Helper.TextBox1.Text
    uiValue = Replace(uiValue, ", ", """, """)
    If Right(uiValue, 1) <> """" Then uiValue = uiValue & """"
    If Left(uiValue, 1) <> """" Then uiValue = """" & uiValue
    
     Set VBProj = Workbooks("Test.XLSB").VBProject 
     Set VBComp = VBProj.VBComponents("uF_Helper") 
    Set CodeMod = VBComp.CodeModule
    
       With CodeMod
                LineNum = 48
                .InsertLines LineNum, "            Case " & uiValue
            End With
        End Sub
    Thanks again everybody for your help; & sharing of your time, & knowledge.

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select Case test String is failing.

    Why are you writing to the VBE?

    What, in words, are you actually trying to do?
    If posting code please use code tags, see here.

  11. #11
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    Hi Norie,

    I have a textBox that a user inputs some Sheet.Names.
    In my Module the caseStatement deletes unprotected sheets.
    I was trying to protect the users desired sheet.Names that were entered into text box.
    It wasnt working in my CaseStatement & thats what I was asking for help with here.

    Lewis AKA [LJMetzger] offered this insight.
    Branches of 'Case Statements' should be constants and not evaluations
    Thats why I am using the VBIDE to write to the Sub before its runs creating the string as a constant & at the end I call another Sub to remove the line that was added.

    Do you have a more elegant solution? Im open to ideas.

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select Case test String is failing.

    You could split the string of sheet names entered by the user to create an array then use Application.Match to check if a sheet name is one of those entered by the user.
    Dim Res As Variant
    Dim ws As Worksheet
    
        arrSheets = Split(uF_Helper.TextBox1.Text, ",")
    
        For Each ws In ActiveWorkbook.Worksheets
            Res = Application.Match(ws.Name, arrSheets, 0)
    
            If Not IsError(Res) Then
                MsgBox ws.Name & " is on the list."
            Else
                ' do something with worksheet
            End If
    
       Next ws

  13. #13
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    Hi thanks StuCram. I didnt even think to try True as the condition. I will check that out as well.

  14. #14
    Forum Contributor dlow's Avatar
    Join Date
    08-12-2013
    Location
    EveryWhere
    MS-Off Ver
    Excel 2007, 2010
    Posts
    275

    Re: Select Case test String is failing.

    Hmm, ok

    mgs73 also suggested that & I tried it, but it didnt work. Could be I didnt try hard enough to see why it didnt work.
    I am gonna try it again thanks Norie & mgs73.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Select Case test String is failing.

    mgs73 did suggest using Split but the rest of the code was quite different to what I'm suggesting.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Checking String Length with Case Select
    By StormStrikes in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-19-2015, 03:04 AM
  2. [SOLVED] Select Case Statement to test for object that may or may not be avalible
    By dlow in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-10-2014, 11:44 AM
  3. [SOLVED] Select Case statements after Search String not working
    By rdowney79 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-10-2013, 03:04 PM
  4. [SOLVED] Very Quick Question-Select Case-Determine if string has delimiters
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-29-2013, 12:40 AM
  5. [SOLVED] Select Case String
    By gmk in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-29-2012, 09:25 PM
  6. Select case to check string array
    By Deamo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-17-2010, 01:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1