AcousticalConsultant
Technical User
Hello,
So essentially what I am trying to do it trap an "insert row" event so that I can prevent it on specific lines, but allow it on others. I know that there is no specific event to trap for insert rows, so we have to go about it another way. One way is to monitor the Worksheet_Change event, but that code would run every single time something is changed in the sheet which kind of bugs me. Instead, I found some interesting code to "redirect" the insert row command to a macro. It works fine on my machine, but when I tried running it on some other computers, it crashes. Here is the code, I'll explain the error afterwards:
Ok, so the "MenuBarInsertRow" object is redirecting the insert row command selected from the menu, and the "RightClickInsertRow" object is redirecting the insert row command selected from the right-click drop-down menu.
The problem is with the right-click control. On some computers, it seems that it can't find the control. So I modified the code to look for it in any command bar as follows:
Even with this code, it doesn't find the control and the RightClickInsertRow object is "Nothing". However, it seems that if I go to a sheet, right-click and select insert row - then try the macro again. Now it works! It's as if I need to 'wake-up' the insert row command for the code to find it. What's going on here? And how can I make sure that I can find this command if I'm distributing this code to others?
Sorry for the long-winded question.
I appreciate any help or insight.
Regards,
AC
So essentially what I am trying to do it trap an "insert row" event so that I can prevent it on specific lines, but allow it on others. I know that there is no specific event to trap for insert rows, so we have to go about it another way. One way is to monitor the Worksheet_Change event, but that code would run every single time something is changed in the sheet which kind of bugs me. Instead, I found some interesting code to "redirect" the insert row command to a macro. It works fine on my machine, but when I tried running it on some other computers, it crashes. Here is the code, I'll explain the error afterwards:
Code:
Dim MenuBarInsertRow As CommandBarControl
Dim RightClickInsertRow As CommandBarControl
Set MenuBarInsertRow = Application.CommandBars("Worksheet Menu Bar").FindControl(ID:=296, Recursive:=True)
Set RightClickInsertRow = Application.CommandBars("Row").FindControl(ID:=3183, Recursive:=True)
MenuBarInsertRow.OnAction = ThisWorkbook.Name & "!CustomInsertRow"
RightClickInsertRow.OnAction = ThisWorkbook.Name & "!CustomInsertRow"
Ok, so the "MenuBarInsertRow" object is redirecting the insert row command selected from the menu, and the "RightClickInsertRow" object is redirecting the insert row command selected from the right-click drop-down menu.
The problem is with the right-click control. On some computers, it seems that it can't find the control. So I modified the code to look for it in any command bar as follows:
Code:
Dim MenuBarInsertRow As CommandBarControl
Dim RightClickInsertRow As CommandBarControl
Set MenuBarInsertRow = Application.CommandBars("Worksheet Menu Bar").FindControl(ID:=296, Recursive:=True)
For Each a In Application.CommandBars
Set RightClickInsertRow = a.FindControl(ID:=3183, Recursive:=True)
If Not RightClickInsertRow Is Nothing Then Exit For
Next a
MenuBarInsertRow.OnAction = ThisWorkbook.Name & "!CustomInsertRow"
If RightClickInsertRow Is Nothing Then
MsgBox "Insert row restrictions will not work"
Else
RightClickInsertRow.OnAction = ThisWorkbook.Name & "!CustomInsertRow"
End If
Even with this code, it doesn't find the control and the RightClickInsertRow object is "Nothing". However, it seems that if I go to a sheet, right-click and select insert row - then try the macro again. Now it works! It's as if I need to 'wake-up' the insert row command for the code to find it. What's going on here? And how can I make sure that I can find this command if I'm distributing this code to others?
Sorry for the long-winded question.
I appreciate any help or insight.
Regards,
AC