Help With Right Click

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim c As Long
Cancel = True
If Not Intersect(Target, Columns("B:B")) Is Nothing Then
Dim Rng As Range, Dn As Range
 Set Rng = Range(Range("C" & Target.Row), Cells(Target.Row, Columns.Count).End(xlToLeft))
    For Each Dn In Rng
        If Dn = "HP" Then c = c + 1
    Next Dn
End If
    MsgBox Target & " Has " & c & " HP's"
End Sub

I was given this code so that when I click on a name in column B it will give a pop up telling me how many 'HPs' are in that row in total, but all the right click options are disabled when I right click elsewhere on the sheet. Can this be fixed so all the normal options are there like , copy, paste etc apart from when I click on a name in column B. Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try:
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Column <> 2 Then Exit Sub
    Cancel = True
    MsgBox Application.CountIf(Target.EntireRow, "HP") & " HP's in row"
End Sub
 
Upvote 0
Try

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim c As Long
Dim Rng As Range, Dn As Range
If Not Intersect(Target, Columns("B:B")) Is Nothing Then
    Cancel = True
    Set Rng = Range(Range("C" & Target.Row), Cells(Target.Row, Columns.Count).End(xlToLeft))
    For Each Dn In Rng
        If Dn = "HP" Then c = c + 1
    Next Dn
    MsgBox Target & " Has " & c & " HP's"
End If
End Sub
 
Upvote 0
Change the code roung like this:-
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Dim num As Long
Dim Rng As Range, Dn As Range
If Not Intersect(Target, Columns("B:B")) Is Nothing Then
  Cancel = True
  Set Rng = Range("C" & Target.Row).Resize(, 230)
     num = Application.CountIf(Rng, "HP")
     MsgBox Target & " Has " & num & " HP's"
End If
End Sub
 
Upvote 0
Thanks all, used Jon Von Der Heydens and that worked.
 
Upvote 0
I have changed the layout of the worksheet because there are not enough columns. I need the code to work for all the names in row 3 rather than column B please.
 
Upvote 0
Try

Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Row <> 3 Then Exit Sub
    Cancel = True
    MsgBox Application.CountIf(Target.EntireRow, "HP") & " HP's in row"
End Sub
 
Upvote 0
Working here. Make sure that events are enabled. Press CTRL + G to open the Immediate Window, type

Application.EnableEvents=True

and press Enter. Then try again.
 
Upvote 0
Got that to work thanks, but it is not countiing how HPs are in that particular column. Also how do I do it so it puts whatever name is in the cell I right click on in row 3 in the MsgBox? eg. "Fred Smith has 8 days booked of so far"
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top