Jim/Dave, thanks so much for your assist. Right after I posted earlier
I figured out what was wrong. And now I have the solution I was lookin
for.
Keep in mind that E15:E3000 will typically be part of a query field.
The idea is that stock numbers are entered in this column. Then
query is run against a text(.txt) file to get price, stock locatio
etc.
Where my right-click menu comes in, is if the user finds out that on
of the stock numbers is invalid. That number can then be corrected, an
rather than run the entire query process, only that cell will b
re-queried.
Otherwise, if it is determined that there is no matching stock number
but that the part number is valid, then clicking on caption "Canno
CrossReference This ..." will force that value to be accepted an
permit manual input of price etc.
Alternatively, the user may decide to add another item, after havin
run the initial query, right-clicking will allow a query just for tha
active cell. Hence the need to show the menu in either Normal View o
in Page Break Preview and whether the cell is a normal cell or part o
a query field.
Right now, I am referencing the various command items as part of th
caption to verify which control is being activated. In real life, th
controls captions will either read "Cannot Cross Reference This Cell
or "Cross Reference This Cell Only", along with the applicable "go"
"no-go" icon.
Without further ado, here's my solution:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVa
Target As Range, Cancel As Boolean)
Dim PBPCellIndex As Long
Dim NormalCellIndex As Long
Dim iCtr As Long
Dim nNV As String
Dim nPBP As String
On Error Resume Next
With Application
.CommandBars("Query Layout").Controls("CrossReference This Quer
Layout Only").Delete
.CommandBars("Query Layout").Controls("Cannot CrossReference Thi
Query Layout").Delete
.CommandBars("Query Layout").Controls("").Delete
.CommandBars("Query").Controls("CrossReference This Quer
Only").Delete
.CommandBars("Query").Controls("Cannot CrossReference Thi
Query").Delete
.CommandBars("Query").Controls("").Delete
End With
If Target.Cells.Count > 1 Then Exit Sub
On Error Resume Next
For iCtr = 1 To 60
If LCase(Application.CommandBars(iCtr).Name) = "cell" Then
If NormalCellIndex = 0 Then
NormalCellIndex = iCtr
With Application.CommandBars
nNV = .Item(NormalCellIndex).Name
'MsgBox ("CrossReference This " & nNV & vbLf & _
"Cannot CrossReference This " & nNV) 'fo
debugging
End With
With Application.CommandBars.Item(NormalCellIndex)
.Controls("CrossReference This nNV " & nNV &
Only").Delete
.Controls("Cannot CrossReference This nNV "
nNV).Delete
.Controls("").Delete
End With
Else
PBPCellIndex = iCtr
With Application.CommandBars.Item(PBPCellIndex)
nPBP = .Name
'MsgBox ("CrossReference This " & nPBP & vbLf & _
"Cannot CrossReference This " & nPBP) 'fo
debugging
.Controls("CrossReference This nPBP " & nPBP &
Only").Delete
.Controls("Cannot CrossReference This nPBP "
nPBP).Delete
.Controls("").Delete
End With
Exit For
End If
End If
Next iCtr
If Not Application.Intersect(Target, Range("E15:E3000")) Is Nothin
Then
If Len(ActiveCell) = 9 And Left(ActiveCell, 2) <> "PN" Then
Wit
Application.CommandBars.Item(NormalCellIndex).Controls.Add(msoControlButton
, , 5, True)
.Visible = True
.TooltipText = "Click this to run CrossReference onl
on This nNV " & nNV
.BeginGroup = True
.Caption = "CrossReference This nNV " & nNV & " Only"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = "CrossReferenceActiveCell"
SheetMaster.Shapes("goCrossReference").Copy
.PasteFace
End With
Wit
Application.CommandBars.Item(PBPCellIndex).Controls.Add(msoControlButton
, , 5, True)
.Visible = True
.TooltipText = "Click this to run CrossReference onl
on This nPBP " & nPBP
.BeginGroup = True
.Caption = "CrossReference This nPBP " & nPBP &
Only"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = "CrossReferenceActiveCell"
SheetMaster.Shapes("goCrossReference").Copy
.PasteFace
End With
With Application.CommandBars("Query
Layout").Controls.Add(msoControlButton, , , 5, True)
.Visible = True
.TooltipText = "Click this to run CrossReference only
on This Query Layout"
.BeginGroup = True
.Caption = "CrossReference This Query Layout Only"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = "CrossReferenceActiveCell"
SheetMaster.Shapes("goCrossReference").Copy
.PasteFace
End With
With
Application.CommandBars("Query").Controls.Add(msoControlButton, , , 5,
True)
.Visible = True
.TooltipText = "Click this to run CrossReference only
on This Query"
.BeginGroup = True
.Caption = "CrossReference This Query Only"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = "CrossReferenceActiveCell"
SheetMaster.Shapes("goCrossReference").Copy
.PasteFace
End With
Else
With
Application.CommandBars.Item(NormalCellIndex).Controls.Add(msoControlButton,
, , 6, True)
.Visible = True
.TooltipText = "This " & nNV & " is not a 9-digit Stock
Number"
.BeginGroup = True
.Caption = "Cannot CrossReference This nNV " & nNV
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = ""
SheetMaster.Shapes("noCrossReference").Copy
.PasteFace
End With
With
Application.CommandBars.Item(PBPCellIndex).Controls.Add(msoControlButton,
, , 6, True)
.Visible = True
.TooltipText = "This " & nPBP & " is not a 9-digit
Stock Number"
.BeginGroup = True
.Caption = "Cannot CrossReference This nPBP " & nPBP
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = ""
SheetMaster.Shapes("noCrossReference").Copy
.PasteFace
End With
With Application.CommandBars("Query
Layout").Controls.Add(msoControlButton, , , 6, True)
.Visible = True
.TooltipText = "This Query Layout is not a 9-digit
Stock Number"
.BeginGroup = True
.Caption = "Cannot CrossReference This Query Layout"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = ""
SheetMaster.Shapes("noCrossReference").Copy
.PasteFace
End With
With
Application.CommandBars("Query").Controls.Add(msoControlButton, , , 6,
True)
.Visible = True
.TooltipText = "This Query is not a 9-digit Stock
Number"
.BeginGroup = True
.Caption = "Cannot CrossReference This Query"
.State = msoButtonUp
.Style = msoButtonIconAndCaption
.OnAction = ""
SheetMaster.Shapes("noCrossReference").Copy
.PasteFace
End With
End If
End If
End Sub
Thanks again for everything.