+ Reply to Thread
Results 1 to 15 of 15

InStr()/Select Case [Excel VBA 2010]

Hybrid View

  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:


        If InStr(ActiveWorkbook.Name, "xxx") > 0 Then
            ActiveSheet.Range("J1").Value = "yyy"
            ActiveSheet.Range("J1").Select
        Else
            If InStr(ActiveWorkbook.Name, "aaa") > 0 Then
                ActiveSheet.Range("F1").Value = "bbb"
                ActiveSheet.Range("F1").Select
            Else
                If InStr(ActiveWorkbook.Name, "ccc") > 0 Then
                    ActiveSheet.Range("F1").Value = "ddd"
                    ActiveSheet.Range("F1").Select
                Else
                    If InStr(ActiveWorkbook.Name, "eee") > 0 Then
                        ActiveSheet.Range("F1").Value = "ffff"
                        ActiveSheet.Range("F1").Select
                    Else
                        If InStr(ActiveWorkbook.Name, "ggg") > 0 Then
                            ActiveSheet.Range("F1").Value = "hhh#"
                            ActiveSheet.Range("F1").Select
                        Else
                            If InStr(ActiveWorkbook.Name, "iii") > 0 Then
                                ActiveSheet.Range("F1").Value = "jjj"
                                ActiveSheet.Range("F1").Select
                            Else
                                If InStr(ActiveWorkbook.Name, "lll") > 0 Then
                                    ActiveSheet.Range("F1").Value = "mmm"
                                    ActiveSheet.Range("F1").Select
                                Else
                                    ActiveSheet.Range("G1").Value = "nnn"
                                    ActiveSheet.Range("G1").Select
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    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
    select case true
    Case ActiveWorkbook.Name like "*xxx*"
    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
    dim vData as variant
    dim n as long
    dim bMatch as boolean
    vData = sheets("Ref").Range("ref_table").value
    for n = lbound(vdata) to ubound(vdata)
        If InStr(ActiveWorkbook.Name, vdata(n, 1)) > 0 Then
            ActiveSheet.Range(vData(n, 3)).Value = vData(n, 2)
            ActiveSheet.Range(vData(n, 3)).Select
            bmatch = true
        end if
       if bmatch then exit for
    next n

  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:

    Sub lg()
        Dim vData       As Variant
        Dim n           As Long
        Dim sName       As String
        Dim wks         As Worksheet
    
        vData = ThisWorkbook.Worksheets("Ref").Range("ref_table").Value
        Set wks = ActiveSheet
        sName = wks.Parent.Name
    
        For n = LBound(vData) To UBound(vData)
            If InStr(sName, vData(n, 1)) Then
                With wks.Range(vData(n, 3))
                    .Value = vData(n, 2)
                    Application.Goto .Cells
                End With
                Exit For
            End If
        Next n
    End Sub
    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.
    vData = Sheets("Ref").Range("ref_table").Value
    Your code is getting a run time error 1004: application defined or object defined error. On this line:
    vData = ThisWorkbook.Worksheets("Ref").Range("ref_table").Value

  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:

    sName = wks.Parent.Name

  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