Option to Enable/Disable Right Click - excel

I want to limit people viewing hidden parts of my excel. To still allow my VBA to hide/unhide columns/sheets as the user clicks through the file I've opted not to protect the workbook structure. The below code works, but I want to put a button in to a hidden sheet to disable this macro if a user knows that it is there (i.e. I want some users to have full control, without having to go change the VBA) - any ideas?
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox ("Sorry Right Click is Disbaled for this Workbook")
End Sub
Thanks,
James

you could place an ActiveX checkbox in the worksheet and use this in your sheet's module:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
If CheckBox1 Then
Cancel = True
MsgBox ("Sorry Right Click is Disbaled for this Workbook")
End If
End Sub

Related

VBA anyway to execute _MouseDown before _Enter in TextBox?

My intention is to open "birthdate" userform when textbox "bdayBox" is entered. And also want to be able to open "birthdate" userform again, if it was closed, by clicking with mouse inside the box, but only if textbox "bdayBox" is currently active.
My problem is, that if the textbox in not active and i click it with mouse, _Enter is executed before _MouseDown. This opens "birthdate" userform twice.
Is there a way to execute _MouseDown before _Enter or any other way to do, what i want without double openings?
*EDIT Before the user even gets to the put in birth date, they select how many people should be added. The form 'birthdate' should only show up, if there are more than 1 person to be added and it should appear automatically, when user is entering the 'bdayBox' textbox. If there is only 1 person, no extra form will appear and user enters birth date in the textbox itself. I want to take the decision from the user away to open the 'birthdate' form or not.
Current code
Public bdayBoxAct As Boolean
Public Sub bdayBox_Enter()
birthdate.Show
bdayBoxAct = True
End Sub
Public Sub bdayBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
bdayBoxAct = False
End Sub
Private Sub bdayBox_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If bdayBoxAct = True Then
birthdate.Show
End If
End Sub
Public Sub UserForm_Initialize()
bdayBoxAct = False
End Sub
I feel this approach is not intuitive for the user, and suffers from the symptoms you are experiencing. My suggestion is to place a button next to the bdayBox textbox, with an appropriate caption telling the user what the button will do, which they can press whenever they need to form to appear. The button replaces the MouseDown logic. Your code then becomes:
Option Explicit
Private Sub bdayBox_Enter()
birthdate.Show
End Sub
Private Sub ShowBirthdate_Click()
birthdate.Show
End Sub

Issue with TAB event in Excel VBA

When user click TAB key from keyboard, some cells should be updated in the same row(Event should be happen iff there is some data in that cell). As of know I written some sample code to display some message. I written code inside the workbook as below
Private Sub Workbook_SheetselectionChange (ByVal Sh As Object, ByVal Target As Range)
Application.EnableEvents = True
Application.OnKey"{TAB}", "sayHi"
End Sub
Sub sayHi()
Application.EnableEvents = True
MsgBox "Hiiii"
End Sub
Issue :- whenever I am clicking TAB key, getting one warning like "cannot run the macro 'C:...sayHi'. The macro may not be available in this workbook or all macros may be disabled".

VBA Code for disable the Right Click option in Excel for the current Workbook only

I have created multiple worksheets in my workbook.
I want to my users are not allowing to see the other sheets in excel.
Since I want to disable the right click option in Excel open event.
I have a piece of code:
Private Sub Workbook_SheetBeforeRightClick (ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Cancel = True
MsgBox ("Sorry Right Click is Disabled for this Workbook")
End Sub
The above code runs only after opening the Workbook and run the Macro.
I want to disable right click option the Workbook when opening the Workbook.
Try this, different parameters...
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub
you can try the codes below: - Good Luck
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.CommandBars("Ply").Enabled = True
End Sub
And
Private Sub Workbook_Open()
Application.CommandBars("Ply").Enabled = False
End Sub

How to create Back button in Excel to move user in the position he came from?

Let me know please if the scenario feasible: user clicks on Sheet1!A1 cell with "Go Details" button and it moves him to Sheet2!B1, where he may click "Back" button in the same row to be moved back to Sheet1!A row he came from. Same way he can click Sheet1!J1 cell with "Go Details" button which moves him to the same Sheet2!B1 row, but this time "Back" button leads him back to Sheet1!J row, so that it remembers the position user came from.
Insert this code in the ThisWorkbook routine:
Private rngLastLink As Range
Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
If UCase(Target.Parent.Value) = "BACK" Then
If rngLastLink Is Nothing Then
Application.EnableEvents = False
Target.Follow
Application.EnableEvents = True
Else
rngLastLink.Worksheet.Activate
rngLastLink.Activate
End If
Else
Set rngLastLink = Target.Parent
End If
End Sub
It will store the cell from any pressed hyperlink that is not called "Back". If "Back" is pressed, it will reactivate this cell.
If the user, selects Excel Options > Quick Acccess Toolbar > All Commands. Add "Back" and "Foward" they will have back and forward navigation after jumping links, just like on a web page.
First you will need to create a new button, then the assign macro screen will pop up once the button is successfully created.
Copy this code
Sub Button1_Click()
Worksheets("sheet2").Activate
End Sub
as macro
change sheet2 as appropriate for your scenario.
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Sheets.Select
ActiveCell.EntireRow.Select
ActiveSheet.Select
will synchronize entire row between worksheets - will only fire when you change worksheets...
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target
As Range)
Sheets.Select
ActiveCell.EntireRow.Select
ActiveSheet.Select
End Sub
will fire every time user change cells in active sheet.
I used Alt-Left arrow to go back.
Sub GoBackToWhereverYouCameFrom()
Application.SendKeys ("%{LEFT}")
End Sub

Excel VBA: Save As triggers Change event in ComboBox

I have an Excel workbook containing some ComboBox controls placed directly on the sheets. These are standard combo boxes from the Forms toolbar.
When the user uses "Save As" to save the workbook with a different name, this triggers the Change event on all the combo boxes, including ones on sheets that aren't active. This seems unreasonable as the selection hasn't actually changed. This causes various undesirable behaviour because of the code in the event handlers. The event isn't triggered on a simple "Save".
Google suggests this is a known problem in Excel. There are rumours that it's caused by using a named range as the ListFillRange for the combo box, which I have done, although it's not a volatile name. I'm looking for a way to prevent this happening with minimal changes to the code and the spreadsheet. Does anyone have a proven solution?
I did the following in a new workbook with only one sheet, Sheet1, and it seemed to work to diable events before save and then reenable them after. It should bypass the problem you see by mimicing an AfterSave event. This is my event code on Sheet1 (could be OLEObject code, too)
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address & ": " & Target.Value
End Sub
This is my ThisWorkbook code
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
' To see the change code work before disabling
' Should show a message box
Sheet1.Range("A1") = "Before After Events Off"
Application.EnableEvents = False
Application.OnTime Now, "ThisWorkbook.Workbook_AfterSave"
' This time it will not show a message box
' You will never see this one . . .
Sheet1.Range("A1") = "After After Events Off"
End Sub
Private Sub Workbook_AfterSave()
Application.EnableEvents = True
End Sub
The .OnTime method throws the AfterSave "event" onto the execution queue. It works!
You could set a flag in the Workbook's BeforeSave event and then check that flag before processing a change event in each of the combo boxes. There does not seem to be an AfterSave event, so you would need to clear the flag after checking it within the combo box change events. The flag would need to be more than a simple boolean since it could not be turned off until all combo box change events were processed. Here's some sample code:
Public ComboBoxChangeCounter As Integer
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Const NumOfComboBoxChangeEvents As Integer = 5
ComboBoxChangeCounter = NumOfComboBoxChangeEvents
End Sub
Function JustSaved() As Boolean
If ComboBoxChangeCounter > 0 Then
ComboBoxChangeCounter = ComboBoxChangeCounter - 1
JustSaved = True
End If
End Function
Private Sub Combo1_Change()
If JustSaved Then Exit Sub
'Your existing code '
' ... '
' ... '
' ... '
End Sub
I set the number of combo box change events as a constant, but there may be some way for you to determine that number programmatically. This workaround does require adding code to every combo box change event, but it should be easy as all you need to do is copy and paste the line If JustSaved Then Exit Sub at the beginning of each event.
This workaround assumes that the Workbook BeforeSave event will get called prior to the combo box change events. I don't know for a fact if that's the case.

Resources