+ Reply to Thread
Results 1 to 6 of 6

Message box when no file is active

Hybrid View

  1. #1
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Message box when no file is active

    How do you show a message box (MsgBox) when no excel file is opened but Excel is running.
    Sometimes I have Excel open but no workbook, I have a couple macros that inform the user that a specific type file is required to run the given macro.
    I am using a tool bar with drop downs for users to select a macro and I just stumbled on this, if I dont have a workbook open the macro that is a message will fail.

    Run-time error '91':
    Object variable or with block variable not set
    Which high-lites the MsgBox portion of code depicting it as failed.
    Option Explicit
    
    Sub Start()
        On Error GoTo EndIt
        If Left(ActiveWorkbook.Name, 6) = "AS9102" Then
            GmMotDataCollectorForm.Show
        Else
    EndIt:
            MsgBox "You must have an active AS9102 excel file open." & _
            vbCrLf & _
            "Example name: ""AS9102 4502730B Y 135-1 100710.xls""" & _
            vbCrLf & _
            "Current file name: " & ActiveWorkbook.Name
        End If
    End Sub
    Edit:
    Remove Color codes
    Last edited by Rick_Stanich; 12-01-2010 at 04:02 PM.
    Regards

    Rick
    Win10, Office 365

  2. #2
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Message box when no file is active

    I think a better solution would be to gray out my custom toolbar if no workbooks are open.

  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,996

    Re: Message box when no file is active

    You could simply have your code check the workbooks.count and react accordingly if it's 0.
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Message box when no file is active

    Rick, please don't add colour to the code. Your choice makes it difficult to read & the tags make copying & pasting to the VB Editor more work
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Message box when no file is active

    romperstomper
    Thank you.

    royUK
    Fixed and thanks for the tip.

  6. #6
    Forum Contributor Rick_Stanich's Avatar
    Join Date
    11-21-2005
    Location
    Ladson SC
    MS-Off Ver
    Office365
    Posts
    1,177

    Re: Message box when no file is active

    Hmmm.
    Not sure how to deal with this.
    No workbook open returns "1" from Workbooks.Count
    One workbook open returns "2" and so on...
    With that I tried the following modified code:
    Option Explicit
    
    Sub Start()
        If Workbooks.Count > "1" Then
            'MsgBox Workbooks.Count 'for testing
            If Left(ActiveWorkbook.Name, 6) = "AS9102" Then
                GmMotDataCollectorForm.Show
            Else
                MsgBox "You must have an active AS9102 excel file open." & _
                       vbCrLf & _
                       "Example name: ""AS9102 4502730B Y 135-1 100710.xls""" & _
                       vbCrLf & _
                       "Current file name: " & ActiveWorkbook.Name
            End If
        Else
            MsgBox "You must have an active AS9102 excel file open." & _
                   vbCrLf & _
                   "Example name: ""AS9102 4502730B Y 135-1 100710.xls""" & _
                   vbCrLf & _
                   "Current file name: " & ActiveWorkbook.Name
        End If
    End Sub
    As long as there is an active workbook it works, if there is no workbook it fails at the msgbox.
    Same error.
    Run-time error '91':
    Object variable or with block variable not set
    Im guessing I cant pass a message when no workbooks are open?


    Edit:
    Just hit my brain stem.
    I have a workbook always open, personal.xls, hidden.
    The logical answer (were working with my brain on this) is to do nothing if there is no open workbook.
    Last edited by Rick_Stanich; 12-01-2010 at 04:02 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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