PC Review


Reply
Thread Tools Rate Thread

Code Only Works on Certain Sheets Within a Workbook

 
 
asmithbcat
Guest
Posts: n/a
 
      15th Oct 2009
I have the following code saved in a file on a network to add an item to the
cell shortcut list once right-clicked.

ThisWorkbook code

Private Sub Workbook_Activate()
AddItemToShortcut
End Sub

Private Sub Workbook_Deactivate()
ResetCellShortcut
End Sub

Sub AddItemToShortcut()

Dim NewItem As CommandBarControl
Set NewItem = CommandBars("Cell").Controls.Add(Type:=1, Before:=1,
Temporary:=True)
NewItem.Caption = "Add New Date"
NewItem.OnAction = "NewDate"
NewItem.BeginGroup = True

End Sub

Sub ResetCellShortcut()

CommandBars("Cell").Reset

End Sub

I believe the "Add New Date" feature should be available on any sheet within
the workbook once a cell is right-clicked. However, this function is only
available on certain sheets within the workbook.

This file is on a network, and multiple users are opening and closing the
file. Could this have lead to the issue? I also have previously saved a
test file on my desktop with the exact same code in it which works fine.
This led me to believe having multiple users open it could have created the
problem.

Thanks
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      15th Oct 2009
I don't think it's the multiple users who are causing the problem.

I'm betting that you're in page break view on the troublesome sheets and in
normal view on the sheets where it works perfectly.

And as user, I wouldn't want you resetting my Cell menu--I may have other stuff
that you'd destroy!

The index for the cell popup in page break view is 3 more than the index in
normal view (but the numbers vary between versions of excel).

I'd use something like this:

Option Explicit
Const myCaption As String = "Add New Date"
Sub AddItemToShortcut()

Dim NewItem As CommandBarControl
Dim myIndex As Long
Dim iCtr As Long

myIndex = Application.CommandBars("Cell").Index

For iCtr = 0 To 3 Step 3
Set NewItem = Application.CommandBars(myIndex + iCtr).Controls.Add _
(Type:=1, Before:=1, Temporary:=True)
NewItem.Caption = myCaption
NewItem.OnAction = "'" & ThisWorkbook.Name & "'!NewDate"
NewItem.BeginGroup = True
Next iCtr
End Sub
Sub ResetCellShortcut()
Dim myIndex As Long
Dim iCtr As Long

myIndex = Application.CommandBars("Cell").Index

On Error Resume Next 'in case it's not there
For iCtr = 0 To 3 Step 3
Application.CommandBars(myIndex + iCtr).Controls(myCaption).Delete
Next iCtr
On Error GoTo 0

End Sub
Sub NewDate()
MsgBox "NewDate"
End Sub


asmithbcat wrote:
>
> I have the following code saved in a file on a network to add an item to the
> cell shortcut list once right-clicked.
>
> ThisWorkbook code
>
> Private Sub Workbook_Activate()
> AddItemToShortcut
> End Sub
>
> Private Sub Workbook_Deactivate()
> ResetCellShortcut
> End Sub
>
> Sub AddItemToShortcut()
>
> Dim NewItem As CommandBarControl
> Set NewItem = CommandBars("Cell").Controls.Add(Type:=1, Before:=1,
> Temporary:=True)
> NewItem.Caption = "Add New Date"
> NewItem.OnAction = "NewDate"
> NewItem.BeginGroup = True
>
> End Sub
>
> Sub ResetCellShortcut()
>
> CommandBars("Cell").Reset
>
> End Sub
>
> I believe the "Add New Date" feature should be available on any sheet within
> the workbook once a cell is right-clicked. However, this function is only
> available on certain sheets within the workbook.
>
> This file is on a network, and multiple users are opening and closing the
> file. Could this have lead to the issue? I also have previously saved a
> test file on my desktop with the exact same code in it which works fine.
> This led me to believe having multiple users open it could have created the
> problem.
>
> Thanks


--

Dave Peterson
 
Reply With Quote
 
asmithbcat
Guest
Posts: n/a
 
      20th Oct 2009
Dave,

You were right on. The sheets that I was having trouble on were in Page
Break view.

As for resetting the cell shortcut menu, no one had changed their menu, but
I've modified the code accordingly just in case.

Thanks for the help!

"Dave Peterson" wrote:

> I don't think it's the multiple users who are causing the problem.
>
> I'm betting that you're in page break view on the troublesome sheets and in
> normal view on the sheets where it works perfectly.
>
> And as user, I wouldn't want you resetting my Cell menu--I may have other stuff
> that you'd destroy!
>
> The index for the cell popup in page break view is 3 more than the index in
> normal view (but the numbers vary between versions of excel).
>
> I'd use something like this:
>
> Option Explicit
> Const myCaption As String = "Add New Date"
> Sub AddItemToShortcut()
>
> Dim NewItem As CommandBarControl
> Dim myIndex As Long
> Dim iCtr As Long
>
> myIndex = Application.CommandBars("Cell").Index
>
> For iCtr = 0 To 3 Step 3
> Set NewItem = Application.CommandBars(myIndex + iCtr).Controls.Add _
> (Type:=1, Before:=1, Temporary:=True)
> NewItem.Caption = myCaption
> NewItem.OnAction = "'" & ThisWorkbook.Name & "'!NewDate"
> NewItem.BeginGroup = True
> Next iCtr
> End Sub
> Sub ResetCellShortcut()
> Dim myIndex As Long
> Dim iCtr As Long
>
> myIndex = Application.CommandBars("Cell").Index
>
> On Error Resume Next 'in case it's not there
> For iCtr = 0 To 3 Step 3
> Application.CommandBars(myIndex + iCtr).Controls(myCaption).Delete
> Next iCtr
> On Error GoTo 0
>
> End Sub
> Sub NewDate()
> MsgBox "NewDate"
> End Sub
>
>
> asmithbcat wrote:
> >
> > I have the following code saved in a file on a network to add an item to the
> > cell shortcut list once right-clicked.
> >
> > ThisWorkbook code
> >
> > Private Sub Workbook_Activate()
> > AddItemToShortcut
> > End Sub
> >
> > Private Sub Workbook_Deactivate()
> > ResetCellShortcut
> > End Sub
> >
> > Sub AddItemToShortcut()
> >
> > Dim NewItem As CommandBarControl
> > Set NewItem = CommandBars("Cell").Controls.Add(Type:=1, Before:=1,
> > Temporary:=True)
> > NewItem.Caption = "Add New Date"
> > NewItem.OnAction = "NewDate"
> > NewItem.BeginGroup = True
> >
> > End Sub
> >
> > Sub ResetCellShortcut()
> >
> > CommandBars("Cell").Reset
> >
> > End Sub
> >
> > I believe the "Add New Date" feature should be available on any sheet within
> > the workbook once a cell is right-clicked. However, this function is only
> > available on certain sheets within the workbook.
> >
> > This file is on a network, and multiple users are opening and closing the
> > file. Could this have lead to the issue? I also have previously saved a
> > test file on my desktop with the exact same code in it which works fine.
> > This led me to believe having multiple users open it could have created the
> > problem.
> >
> > Thanks

>
> --
>
> Dave Peterson
> .
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Microsoft Excel Discussion 6 29th Mar 2006 12:43 PM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Microsoft Excel Programming 6 29th Mar 2006 12:43 PM
How to repeat a code for selected sheets (or a contiguous range of sheets) in a Workbook? Dmitry Microsoft Excel Worksheet Functions 6 29th Mar 2006 12:43 PM
Macro for filter on protected workbook that works for all sheets, no matter what sheets are named? StargateFanFromWork Microsoft Excel Programming 6 26th Jan 2006 06:31 PM
run code on opening workbook and apply code to certain sheets =?Utf-8?B?SmFuZQ==?= Microsoft Excel Programming 7 8th Aug 2005 09:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 AM.