Accommodate two functions in Sub Workbook_SheetBeforeRightClick

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi, What should be modified to run the two functions in workbook module.

VBA Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

  Select Case Sh.Name
    Case "Agents"
      Exit Sub
    Case Else
  End Select
 
  'Insert Screenshot and send Email in Column E
  If Target.Column <> 5 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then Exit Sub
  Cancel = True
  Call Module3.SelectOLE3
 
 
  'Copy REQ reference and Open Remedy in Column D
  If Target.Column <> 4 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then Exit Sub
  Cancel = True
  Call Module6.CopyREQ6
    
 
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Give this a try with a copy of your workbook.

VBA Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

  If Sh.Name <> "Agents" Then
 
    'Insert Screenshot and send Email in Column E
    If Target.Column = 5 And Application.CountA(Cells(Target.Row, 1).Resize(, 2)) = 2 Then
      Cancel = True
      Call Module3.SelectOLE3
    
    'Copy REQ reference and Open Remedy in Column D
    ElseIf Target.Column = 4 And Application.CountA(Cells(Target.Row, 1).Resize(, 2)) = 2 Then
      Cancel = True
      Call Module6.CopyREQ6
    End If
  
  End If
    
End Sub
 
Upvote 0
Hi Peter_SSs, I modified yours and it works. Is there any way to make it more neatly.

VBA Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)

  Select Case Sh.Name
    Case "Agents"
      Exit Sub
    Case Else
  End Select
 
  If Target.Column <> 4 And Target.Column <> 5 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then Exit Sub
 
  'Insert Screenshot and send Email in Column E
  If Target.Column = 5 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then
  Cancel = True
  Call Module3.SelectOLE3
 
  End If
 
  'Copy REQ reference and Open Remedy in Column D
  If Target.Column = 4 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then
  Cancel = True
  Call Module6.CopyREQ6
    
  End If
 
 
End Sub
 
Upvote 0
Hi,​
remove Select Case like Peter did and remove the CountA part within each Target.Colum codeline …​
Using Select Case for the Target.Column should be smart …​
 
Last edited:
Upvote 0

Correction for previous post : Using Select Case for the Target.Column should be smart …
As after the first If you know the column is 4 or 5 so you can apply the Cancel before the first If Target.Column …​
 
Upvote 0
According to the last 3 posts :​
VBA Code:
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    If Sh.Name <> "Agents" And Target.Column > 3 And Target.Column < 6 Then _
        If Application.CountA(Cells(Target.Row, 1).Resize(, 2)) = 2 Then _
            Cancel = True: If Target.Column = 4 Then Module6.CopyREQ6 Else Module3.SelectOLE3
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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