Consulting

Results 1 to 8 of 8

Thread: Workbook Partial Name Match

  1. #1
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location

    Workbook Partial Name Match

    All I want to do is the check that the workbook contains the words AddIn in it's name. If it does activate it, if not end the sub. I can't get it right. Latest attempt doesn't work.
    If wb.Name Like "*AddIn*" Then
            wb.Activate
                Exit Sub
                    End If

    Error = "End if without block if".


    Any help appreciated

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    At first glance, that bit looks OK

    Maybe it's in the other code?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    The complete code:
    Sub StartFunctional()
    Dim wb As Workbook
    Dim Answer As VbMsgBoxResult
    Dim wsJournal As Worksheet
    
        With ThisWorkbook
        
        If wb.Name Like "*AddIn*" Then
            wb.Activate
                Exit Sub
                    End If
        End With
        
        Set wsJournal = Worksheets("Journal")
        
            With wsJournal
            .Cells(4, 6).NumberFormat = "@"
            End With
    
        Answer = MsgBox("Existing data will be cleared. Are you sure?", vbYesNo, "Create Journal Template")
            If Answer = vbYes Then
                UserForm1.Show
            End If
    End Sub

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    1. You're not Set-ing wb

    2. The "With ThisWorkbook" is unnecessary since you use wb.Activate, and not something like just .Activate to default to the With ThisWorkbook

    Don't know - all your With's, If's, and End's seem to pair up


    Option Explicit
    
    Sub StartFunctional()
    
        Dim wb As Workbook
        Dim Answer As VbMsgBoxResult
        Dim wsJournal As Worksheet
    
        With ThisWorkbook
            If .Name Like "*AddIn*" Then
                .Activate
                Exit Sub
            End If
        End With
        
        Set wsJournal = Worksheets("Journal")
        
        With wsJournal
            .Cells(4, 6).NumberFormat = "@"
        End With
    
        Answer = MsgBox("Existing data will be cleared. Are you sure?", vbYesNo, "Create Journal Template")
        If Answer = vbYes Then
            UserForm1.Show
        End If
    End Sub
    
    Maybe some one else can see something
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    Thanks! That seems to work, except that it's exposed another problem in that the macro doesn't run even though the workbook name
    has <AddIn> in it's name, the procedure does not execute. If I take this piece of code out, then it does.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Confused

    The macro HAS to run in order to test if "AddIn" is in the workbook's name

    If it is, then the sub exits
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    Nov 2018
    Location
    London, U.K.
    Posts
    99
    Location
    Ah right, my bad. If it has AddIn in the name then I want to sub to continue not exit.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    If you're checking to see if the macro is running from an add in ( .XLAM) then you can use

    If ThisworkBook.IsAdd Then
    
    ...
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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