+ Reply to Thread
Results 1 to 7 of 7

Disable Insert button code works fine, but some times throws Run-Time error '5'

  1. #1
    Registered User
    Join Date
    11-17-2016
    Location
    Hyderabad
    MS-Off Ver
    Office 2007
    Posts
    4

    Disable Insert button code works fine, but some times throws Run-Time error '5'

    Hi,
    Below is my code in sheet1 . Sometimes, this is working fine( Disabling "Insert" menu option ). But when I close and reopen the Excel/view another sheet and revisit sheet1 , Run-Time error '5' with "Invalid procedure call or argument" error is popping up at Application.CommandBars block. There is no consistency in the error line . Could some one please help me out on how to fix this issue. Thanks in advance.

    Public Sub Worksheet_Activate()
    With CommandBars("Worksheet Menu Bar")
    .Controls("Insert").Controls("Rows").Enabled = False
    .Controls("Insert").Controls("Columns").Enabled = False
    End With
    With Application.CommandBars("Row")
    .Controls("Insert").Enabled = False
    End With
    With Application.CommandBars("Cell")
    .Controls("&Insert...").Enabled = False
    End With
    End Sub

    Private Sub Worksheet_Deactivate()
    With CommandBars("Worksheet Menu Bar")
    .Controls("Insert").Controls("Rows").Enabled = True
    .Controls("Insert").Controls("Columns").Enabled = True
    End With
    With Application.CommandBars("Row")
    .Controls("Insert").Enabled = True
    End With
    With Application.CommandBars("CELL")
    .Controls("&Insert...").Enabled = True
    End With
    End Sub
    Last edited by challasl; 11-17-2016 at 09:05 AM.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    The Insert option for the Row Commandbar also uses the '&' accelerator same as the option in the Cell commandbar.

  3. #3
    Registered User
    Join Date
    11-17-2016
    Location
    Hyderabad
    MS-Off Ver
    Office 2007
    Posts
    4

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    Thanks for the quick reply. So for Row command bar, should I use Controls("&Insert...").Enabled = <XXX> or just Controls("&Insert").Enabled = <XXX>
    Please suggest as I am new to VBA.

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    Why don't you simply just try it? It would give you an answer a lot quicker than posting here and waiting for someone to reply.

  5. #5
    Registered User
    Join Date
    11-17-2016
    Location
    Hyderabad
    MS-Off Ver
    Office 2007
    Posts
    4

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    Hi, I have tried with both "&Insert" and "&Insert..." . While "&Insert..." was throwing Run-Time '5' error on both my local machine and VM ,
    "&Insert" was working on my Local machine Excel, but still throwing same error in my VM. Both the machines have Excel 2007.

    This issue seems to be very weird as same code is working fine at times and suddenly RunTime error 5 pops up. Could you please Guide. Thanks.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    Sorry, I can't help with the Virtual Machine issue.

    All I can say is the menu name must exactly match the caption displayed including any trailing ellipses, if they are used. The ampersand should be placed before the underlined letter, if any.

  7. #7
    Registered User
    Join Date
    11-17-2016
    Location
    Hyderabad
    MS-Off Ver
    Office 2007
    Posts
    4

    Re: Disable Insert button code works fine, but some times throws Run-Time error '5'

    Thanks for your support. I was finally able to fix this issue with below code change. Seems like hardcoding the menu option name was creating the problem..
    Set CtrlMenu = Application.CommandBars("Row")
    For Each Item In CtrlMenu.Controls
    If Item.Caption Like "*Insert*" Then
    Item.Enabled = False
    Exit For
    End If
    Next

    Set CtrlMenu = Application.CommandBars("cell")
    For Each Item In CtrlMenu.Controls
    If Item.Caption Like "*Insert*" Then
    Item.Enabled = False
    Exit For
    End If
    Next

+ 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. error on disable/insert row disable code
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-09-2014, 05:15 AM
  2. [SOLVED] VBA Program works some times and some times gives me a Run-time error '1004'?
    By redsab in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-13-2014, 11:15 PM
  3. vba code throws an error first time but runs perfectly the second time. HELP!!
    By mvneema in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-29-2014, 12:42 PM
  4. [SOLVED] Code Gives error if I run it from Module but works fine when run it from This Worksheet
    By VKS in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-17-2013, 01:18 PM
  5. Getting this code to work on MAC (ERROR 68) - works fine on Windows
    By LT1511 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2012, 11:09 PM
  6. macro works fine once than scrambles everything the second time
    By drocket in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-30-2007, 04:49 PM

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