Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8589617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:06:01+00:00 2026-06-11T23:06:01+00:00

Private Sub Workbook_BeforeSave… MsgBox Why If Form1 is focused – the MsgBox doesn’t appear.

  • 0
Private Sub Workbook_BeforeSave...  
MsgBox "Why"

If Form1 is focused – the MsgBox doesn’t appear. Saving procedure is canceled.
Plese don’t tell me that the end user must click outside the form to save the file.
Form1 is not modal.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:06:02+00:00Added an answer on June 11, 2026 at 11:06 pm

    please place a textbox on a form, place the cursor inside it, or just
    click on form header. Then press Ctrl+S, and see if the MsgBox appears
    or not. – Alegro 49 mins ago

    In such a case simply paste this code in your userform. This code is slightly modified from my other answer.

    CODE (Tried And Tested):

    Private Declare Function GetKeyboardState _
    Lib "user32" (pbKeyState As Byte) As Long
    
    Private Myarray(255) As Byte
    
    Dim Cntrl_Key As Boolean, Sletter_Key As Boolean
    
    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer)
        If KeyCode = 17 Then Cntrl_Key = True
        If KeyCode = 83 Then Sletter_Key = True
        Call ShowKey
    End Sub
    
    Private Sub RefreshKeyState(RefreshState As Boolean)
       If RefreshState Then
          Call GetKeyboardState(Myarray(0))
       End If
    End Sub
    
    Private Sub ShowKey()
        '~~> Check for Ctrl + S
        If Cntrl_Key = True And Sletter_Key = True Then
            '~~> Save Workbook
            ActiveWorkbook.Save
            Cntrl_Key = False
            Sletter_Key = False
        End If
    End Sub
    

    SCREENSHOT:

    enter image description here

    NOTE: This is not a foolproof method as a user may press CTRL and then types something else and then presses the “S” key then the code will still fire. We can include a small check to see if the next key pressed after CTRL was pressed is S or not. If not then set it to false again… For example

    Private Declare Function GetKeyboardState _
    Lib "user32" (pbKeyState As Byte) As Long
    
    Private Myarray(255) As Byte
    
    Dim Cntrl_Key As Boolean, Vletter_Key As Boolean
    Dim OtherLetter As Boolean
    
    Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, _
    ByVal Shift As Integer)
        If KeyCode = 17 Then Cntrl_Key = True
        If KeyCode = 83 Then Vletter_Key = True
        If KeyCode <> 83 And KeyCode <> 17 Then
            OtherLetter = True
        End If
        Call ShowKey
    End Sub
    
    Private Sub RefreshKeyState(RefreshState As Boolean)
       If RefreshState Then
          Call GetKeyboardState(Myarray(0))
       End If
    End Sub
    
    Private Sub ShowKey()
        '~~> Check for Ctrl + S
        If Cntrl_Key = True And OtherLetter = True Then
            Cntrl_Key = False
        ElseIf Cntrl_Key = True And Vletter_Key = True Then
            '~~> Save Workbook
            ActiveWorkbook.Save
            Cntrl_Key = False
            Vletter_Key = False
        End If
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this doesn't feel right Private Sub drop_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
I made this procedure to help me re-use a select query: Private Sub selectQry(ByVal
Private Sub Workbook_Open() Sheets(01).Delete Sheets(02).Copy before:=Sheets(02) Sheets(02 (2)).Name = 01 Form1.Show End Sub Form1
Private Sub Command1_Click() Dim x As Integer For x = 1 To 100 List1.AddItem
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim allowedChars
Private Sub Command1_Click() Dim dom As New DOMDocument Dim http As New XMLHTTP Dim
Private Sub cmdLogin_Click() On Error GoTo ErrorHandler Dim RowNo As Long Dim Id As
My Code: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using the following code: Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer
I currently have this code : Private Sub Worksheet_Change(ByVal Target As Range) Dim lastrow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.