I have an excel sheet which is protected, since the sheet is protected I don't want the user to save it, and I don't want the save the sheet prompt to appear when someone closes the workbook. Till now I have been using this:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
Application.DisplayAlerts = False
End Sub
Using this code, save feature is disabled but the prompt is still appearing
Similar problem: Since the workbook is protected whenever someone tries to change the cell content it displays an alert, I want to disable that prompt message as well.
Can someone help me to fix this
L42 has already answered part of your question.
I want to disable that prompt message as well.
Do this. While protecting the sheet, unckeck the option Select Locked Cells. Now that will take care of the keyboard input while the sheet is locked and protected.
As for mouse inputs i.e the prompt showing up when you double click on the cell, use this :)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub
Related
I want a MsgBox to appear when cell A1 is clicked. I want it to appear even if A1 is already active when it is clicked:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If Target.Row = 1 And Target.Column = 1 Then
MsgBox ("message")
End If
Application.EnableEvents = True
End Sub
This code works only if cell A1 is not already selected when I click on it. Currently the message box does not appear in this case.
Is there a way to fix this?
Your code is using Worksheet_SelectionChange which only fires when a different cell is selected (hence the name Selection Change).
Alternatively, if it's okay if your [unknown] goal is attained using double click or right click then there are other worksheet events that will help:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox Target.Address & " was double clicked"
Cancel = True 'don't edit cell
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
MsgBox Target.Address & " was right clicked"
Cancel = True 'don't open context menu
End Sub
Note that the code for these event procedures need to be placed in the worksheet module.
Edit: More creative ways
Click Event via PeekMessage API
If it must be a single click, there are "sneakier" ways to accomplish this, such as adding a Click event. This is not a built-in feature of Excel VBA, and thus, this method is not generally recommended.
It involves checking for the WM_MOUSEMOVE message when a cell is mouse-clicked, which is accomplished by calling the PeekMessage API inside the Worksheet_SelectionChange event. More info and examples here.
Transparent command button
There could also be a round-about way to accomplish this using an ActiveX Command Button with no caption, with the BackStyle property set to frmBackStyleTransparent.
Neither of these methods have been tested and you might need to do some fancy coding to get them to work. Depending on how often the same cell will be clicked that is already selected (and therefore how that functionality is to you), you may want to simply re-think the layout of your worksheet.
For example, you could add an extra column and have the user click the cell next to the one with the value to activate your message box.
More Information:
MSDN : Worksheet.BeforeDoubleClick Event (Excel)
MSDN : Worksheet.BeforeRightClick Event (Excel)
Chip Pearson : Events in Excel VBA
I would like a dialog box to appear when someone attempts to close an important Excel sheet. Namely I would like it to ask "Have you logged all Changes?" giving them the opportunity to click "Yes" or "No". If they click "Yes" the sheet will close, but if they Click "No" it will ask them to "Please log all changes in the Change Notes section".
Any and all help greatly appreciated.
This is the code you are looking for. It asks the question, and if No is clicked, it cancels the closing of the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ureply = MsgBox("Have you Saved All Changes?", vbYesNo)
If ureply = vbNo Then
Cancel = True
End If
End Sub
This code needs to go on 'ThisWorkbook' (Shown above the word 'Modules') on your VBA editor
Q: What is recommended way of notifing user about loss of functionality unless User change macro security settings?
What I do now:
I display warning on the first sheet user see after opening Workbook, with explanation why things WONT work unless proper settings are set.
And I hide it on start up. (Which wont happen unless settings are OK)
But its not perfect solution:
That message is just one time. (While user could send that sheet to somebody else with different settings...)
Hiding and showing those few rows is treaded by Excel as changing document. (So just opening and closing excel will generate Save changed warning!)
Ok, here is best answer I could think off
(And big thx to #brettdj for suggestion!)
Sub HideMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = True
ThisWorkbook.Saved = Status
End Sub
Sub ShowMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = False
ThisWorkbook.Saved = Status
End Sub
Those are for showing and hiding macros. Saved state is preserved so only user actions can trigger "unsaved changes" dialogbox.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_Open()
Call HideMacroSecWarning
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call HideMacroSecWarning
End Sub
Those assure that no matter the state of user settings warning will ALWAYS be displayed in SAVED FILE. (So even if for user Exel do not display that warning, somebody else who will open it on different machine will see that warning.)
I have some code that reacts to a double click. This action works just fine, my question though.. after filling in the form that pops up due to the double click action the form is unloaded, no problem here, but the user is left in edit mode inside the cell he double clicked on.
Now normally in Excel you can press CTRL-Enter and the cell will just simply be selected and you out of edit mode. How do I achieve this in VBA ?
All I can seem to work is moving the cell up one, but really just want the user to exit Edit mode and stay on the current cell, the one he double clicked on.
Private Sub Cancel_Click()
Cells(ActiveCell.Column, ActiveCell.Row - 1).Activate
Cells(ActiveCell.Column, ActiveCell.Row).Activate
Unload Term
End Sub
Any suggestions ?
Use the BeforeDoubleClick's Cancel argument, which ... cancels clicking into the cell:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
UserForm1.Show
End Sub
I have an add-in with an application object. The object is declared WithEvents. This enables me to flush some data points to a central database every time the user saves the file. This works most of the time. However, there is one user who quits the Excel application, which calls up the Save dialog box. It appears that quitting Excel with an unsaved file means that the WorkbookBeforeSave event does not fire.
Just to emphasize, I have confirmed that the event does fire when the user hits CTRL-S, or presses the save button. I have also confirmed that the event does NOT fire if I quit the application.
I can think of a few workarounds (automatically save every 10 seconds, for example), but I'm not crazy about that. Can anyone confirm this behavior and/or does anyone have a remedy?
Option Explicit
Private WithEvents mapp As Excel.Application
Private Sub mapp_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
' Sanity preservation device
Msgbox "WorkbookBeforeSave event fired."
SaveSomeData Wb
End Sub
You could use the Workbook_BeforeClose routine and the Workbook Object's Saved property.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If ThisWorkbook.Saved = False Then
'Call save function
End If
End Sub
Have you considered using before BeforeClose? This might be a better alternative.