+ Reply to Thread
Results 1 to 15 of 15

Select Case test String is failing.

  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:
    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    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,594

    Re: Select Case test String is failing.

    Try
    Please Login or Register  to view this content.

  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.

    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis
    Attached Files Attached Files

  8. #8
    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:
    Please Login or Register  to view this content.
    Thanks again everybody for your help; & sharing of your time, & knowledge.

  9. #9
    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.

  10. #10
    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.

  11. #11
    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.
    Please Login or Register  to view this content.

  12. #12
    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….
    Please Login or Register  to view this content.

    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.

  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.

    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.

  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.

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

  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