Consulting

Results 1 to 11 of 11

Thread: Solved: Hide UserForm & Stop Closing Excel

  1. #1
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location

    Solved: Hide UserForm & Stop Closing Excel

    Hi All
    I have a multipage user form that opens directly from workbook open procedure, at the moment the user cannot access excel without closing the form, the user wants to open various other excel spreadsheets at the same time

    I can hide the form by using the code below and unhide the form from the prompt page which is visible & protected, all other sheets are hidden.
    [vba]
    Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    Me.Hide
    End Sub
    [/vba]
    The problem it leaves the user in the excel?
    How can i make the user return to the form, without closing the workbook that this form belongs to? but the can close any other excel workbook.
    Have also tried having the form vbmodeless
    Hope thats clear
    Rob

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings,

    Quote Originally Posted by Rob342
    ...The problem it leaves the user in the excel?
    I am not following. I can hide a userform (vbModal) and switch applications w/o issue. Actually, the form can be displayed and I can still switch to another application, so could you re-explain this?

    Quote Originally Posted by Rob342
    ...How can i make the user return to the form, without closing the workbook that this form belongs to?

    but the can close any other excel workbook.
    I am afraid I am not following this part at all

    Could you attach a small workbook, .xls format preferably, showing the basic issue? Maybe that will help us see what you are trying to solve

    Mark

  3. #3
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    HI Mark

    Thanks for the reply
    Have attached a small example, the form is full screen and because there is no min button you cannot make it smaller, so i need to hide it and make the user return to the form, without closing the workbook that this form belongs to.

    Rob

    ps if you want the full WB can i send this PM
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    This might be sufficient:

    [VBA]
    Private Sub CommandButton8_Click()
    Hide
    ThisWorkbook.Save
    End Sub
    Private Sub UserForm_Initialize()
    With Application
    .WindowState = xlMaximized
    Move 0, 0, .Width, .Height
    End With
    End Sub
    [/VBA]

  5. #5
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    thanks snb

    But how does the user get the form to reappear? and it does not stop the user hitting the X button in excel to close excel down without returning to the form?
    Rob

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Insert an object in the sheet or use doubleclick in a certain cell:

    [vba]
    Private commandbutton1_Clcik()
    userform1.show
    end sub
    [/vba]

    NB. userform1.show :
    - loads the userform
    - shows the userform

    if the userform has been hidden, userform1.show
    - doesn't load the userform again but only
    - shows the userform (retaining the most recent values of the userform controls).

  7. #7
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    thanks snb

    i did originally try this, but thought there might be something more elegant
    is there no way to stop the user hitting the X button to close the sheet or can you set Workbook before close to cancel =false ?

    Rob

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Rob342
    thanks snb

    i did originally try this, but thought there might be something more elegant
    is there no way to stop the user hitting the X button to close the sheet or can you set Workbook before close to cancel =false ?

    Rob
    You could disable the the workbook's close button, but really just blocking the action would seem much more effective/easier to me.

    In the ThisWorkbook Module:

    [VBA]Option Explicit

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim i As Long

    For i = 0 To UserForms.Count - 1
    If UserForms(i).Name = "frmTest" Then
    Cancel = True
    UserForms(i).Show
    Exit For
    End If
    Next
    End Sub[/VBA]

    In the UserForm's Module:
    [VBA]Option Explicit

    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)

    If CloseMode = vbFormControlMenu Then
    Cancel = True
    MsgBox "Please use the Cancel or Exit Button !"
    End If
    End Sub

    Private Sub cmdExit_Click()
    Unload Me
    ThisWorkbook.Save
    ThisWorkbook.Close False
    End Sub

    Private Sub cmdHide_Click()
    Me.Hide
    End Sub

    Private Sub UserForm_Initialize()
    Dim lngWinState As XlWindowState
    Dim ws As Worksheet
    Dim IRow As Long

    With Application
    .ScreenUpdating = False
    lngWinState = .WindowState
    .WindowState = xlMaximized
    Me.Move 0, 0, .Width, .Height
    .WindowState = lngWinState
    .ScreenUpdating = True
    End With
    End Sub

    Private Sub UserForm_Resize()
    Me.cmdHide.Top = (Me.Height - Me.cmdHide.Height) - 80
    Me.cmdHide.Left = Me.Width - Me.cmdHide.Width - 26
    Me.cmdExit.Top = Me.cmdHide.Top + Me.cmdHide.Height + 6
    Me.cmdExit.Left = Me.cmdHide.Left
    End Sub[/VBA]

    Please note that added a userform and am simply using the form's Name property to see if it still exists.

    Hope that helps,

    Mark
    Attached Files Attached Files

  9. #9
    VBAX Mentor
    Joined
    Apr 2009
    Location
    Kingsbury
    Posts
    423
    Location
    Thanks Mark
    That works just the way i want it to.
    most appreciated
    Rob

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Quote Originally Posted by Rob342
    Thanks Mark
    That works just the way i want it to.
    most appreciated
    Rob
    You are most welcome and glad it worked :-)

  11. #11
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    an alternative approach
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •