+ Reply to Thread
Results 1 to 15 of 15

InStr()/Select Case [Excel VBA 2010]

  1. #1
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    InStr()/Select Case [Excel VBA 2010]

    I have the following code:


    Please Login or Register  to view this content.
    I'd like to use Select Case but am unsure how to set it up using InStr(). Any help is appreciated.

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr()/Select Case [Excel VBA 2010]

    why do you want to use select case? I can't see any benefit from it but you would need to use
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    There are several workbooks that will be created and I have to keep adding to this list. The location of the specific cell changes periodically due to formatting issues. Trying to clean up the code and not have a bazillion nested If/Thens.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr()/Select Case [Excel VBA 2010]

    then you ought to use a reference table that you can loop through in my opinion. select case will be just as untidy as if..elseif

  5. #5
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    Fair Enough.

    Can you provide an example of a reference table & loop?

    I am not a programmer by nature. Taught myself a few things, learn stuff from people on here, but not very good at it. Nor is it consistent. Any help is appreciated.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: InStr()/Select Case [Excel VBA 2010]

    assuming a three column table on sheet Ref with the name criterion in column 1, the output value in column 2 and the output cell in column 3
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    The code in question is in an Add-in I created. I have been unsuccessful in getting the table onto a worksheet within it.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: InStr()/Select Case [Excel VBA 2010]

    Create a worksheet named Ref in the add-in, add your data there as Joseph suggested, add a named range to reference it, and put this code (modestly adapted from Joseph's, and completly untested) in the add-in:

    Please Login or Register  to view this content.
    Last edited by shg; 10-09-2012 at 10:36 AM.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    I know how to add a worksheet except everything is greyed out. I simply am unable to do so. No idea why either.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: InStr()/Select Case [Excel VBA 2010]

    Select the add-in's VBA project in the Project Explorer Window, select the ThisWorkbook module, in the Properties window set the IsAddin property to False, and proceed from there. Set it back to True when you're done.

  11. #11
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    Done & Done.
    However, there are two issues.

    On his code I get a run time error 9: subscript out of range.
    Please Login or Register  to view this content.
    Your code is getting a run time error 1004: application defined or object defined error. On this line:
    Please Login or Register  to view this content.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: InStr()/Select Case [Excel VBA 2010]

    Lloyd, if the code is in the add-in, and worksheet "Ref" is in the add-in, and Sheets("Ref") refers to the active workbook (which can NEVER be an add-in), which of those codes do you think is correct?

    Did you create the named range "ref_table"?

  13. #13
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    Obviously the one that refers "Ref" being in the add-in.

    I did but I may have misentered the scope whilst doing so. I'll have to check it on the morrow.

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: InStr()/Select Case [Excel VBA 2010]

    Note the change in the code in the prior post:

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    03-22-2012
    Location
    OR, USA
    MS-Off Ver
    Excel 14/2010
    Posts
    273

    Re: InStr()/Select Case [Excel VBA 2010]

    It's working now. Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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