I have a userform which is created in vba and I want to display only the userform when I open my excel... is there a way to do this.. I have already tried the codes such as
application.visible = false , activewindow.visible= false
if I use this codes in the module before open the files which are already open will b hidden along with the file which I am opening
can someone tel me how can i particularly hide the file which I want to open and display the userform
Try something like this
1- Create a user form with 2 button (see below pic)
2- ThisWorkbook code
Private Sub Workbook_Open()
UserForm1.Show vbModeless
End Sub
3- Form code
Private Sub CommandButton1_Click()
If Workbooks.Count > 1 Then
Windows("Test.xlsm").Visible = True
Else
Application.Visible = True
End If
End Sub
Private Sub CommandButton2_Click()
If Workbooks.Count > 1 Then
Windows("Test.xlsm").Visible = False
Else
Application.Visible = False
End If
End Sub
Private Sub UserForm_Initialize()
If Workbooks.Count > 1 Then
Windows("Test.xlsm").Visible = False
Else
Application.Visible = False
End If
End Sub
Private Sub UserForm_Terminate()
If Workbooks.Count > 1 Then
Windows("Test.xlsm").Visible = True
Else
Application.Visible = True
End If
End Sub
This will only show or hide the form's workbook. Any other workbooks opened will remain unaffected.
I think if you ensure that other workbooks are not open, that might solve the problem.
Private Sub Workbook_Open()
If Workbooks.Count > 1 Then
MsgBox "Close All excel files before running it"
ThisWorkbook.Close
Else
Application.Visible = False
frmmain.Show
End If
End Sub
Related
Does anyone know any VBA code that will close and save an excel file after a delay? I tried some kutools code that was supposed to close only after some idle time but it closes without checking for inactivity.
Paste in Routine Module:
Option Explicit
Const idleTime = 30 'seconds
Dim Start
Sub StartTimer()
Start = Timer
Do While Timer < Start + idleTime
DoEvents
Loop
'///////////////////////////////////////////////////////
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'Step 1: Declare your variables
Dim ws As Worksheet
'Step 2: Unhide the Starting Sheet
Sheets("Sheet1").Visible = xlSheetVisible
'Step 3: Start looping through all worksheets
For Each ws In ThisWorkbook.Worksheets
'Step 4: Check each worksheet name
If ws.Name <> "Sheet1" Then
'Step 5: Hide the sheet
ws.Visible = xlVeryHidden
End If
'Step 6: Loop to next worksheet
Next ws
'Application.ScreenUpdating = True
Range("A1").Select
ThisWorkbook.Save
'Application.DisplayAlerts = True
'//////////////////////////////////////////////////////////
'Application.DisplayAlerts = False
Application.Quit
ActiveWorkbook.Close SaveChanges:=True
Application.DisplayAlerts = True
End Sub
Paste in ThisWorkbook :
Option Explicit
Private Sub Workbook_Open()
StartTimer
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
StartTimer
End Sub
Paste in Routine Module:
Sub Reset()
Static SchedSave
If SchedSave <> 0 Then
Application.OnTime SchedSave, "SaveWork", , False
End If
SchedSave = Now + TimeValue("00:10:00") '<--- Ten minutes
Application.OnTime SchedSave, "SaveWork", , True
End Sub
Sub SaveWork()
MsgBox "Run the close workbook macro here."
'ThisWorkbook.Save
'Application.Quit
'ThisWorkbook.Close
End Sub
Paste in ThisWorkbook:
Private Sub Workbook_Open()
Reset
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Reset
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Reset
End Sub
Timer will start automatically when workbook is opened. Presently set for 10 minutes (can be adjusted). Closing macro code has been disabled and presently replaced with a MsgBox notice.
I'm trying to create a control that requires a user to enter information in a specific cell before they close the workbook. If the cell is empty when the users attempts to close then they should be prompted to either stay in the workbook and enter information or exit without saving. If the cell is populated then the workbook should automatically save itself.
Below is what I managed to come up with so far, placed in the ThisWorkbook object. The issue I'm having is that after the MsgBox appears and an option is selected, it then reappears a second time. I can't work out why this is happening so hopefully someone on here can point out what it is I'm missing.
Note, I only want the current active workbook to close, not the entire application to quit. So if the user has other Excel windows open I don't want those to get closed also.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Range(“A1”).Value = “” Then
OutPut = Msgbox (“A1 is empty. Exit without saving?”, vbOKCancel + vbDefaultButton2)
If OutPut = 1 Then
ThisWorkbook.Close False
Else: Cancel = True
Exit Sub
End If
End If
ActiveWorkbook.Save
End Sub
Well, you do try to close the workbook again using ThisWorkbook.Close False, that's where the second event originates from.
Instead, use ThisWorkbook.Saved = True to prevent the confirmation dialog to pop up:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim OutPut As VbMsgBoxResult
If Range("A1").Value = "" Then
OutPut = MsgBox("A1 is empty. Exit without saving?", vbOKCancel + vbDefaultButton2)
If OutPut = vbOK Then
ThisWorkbook.Saved = True
Else
Cancel = True
End If
Else
ThisWorkbook.Save
End If
End Sub
I have a userform and would like this to be the first thing that is shown to the user when opening the workbook, and the sheet behind this form to be hidden.
I understand the below is the code to do this:
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show vbModeless
End Sub
This performs the operation successfully, but my worksheet flashes up for a second or two before it is hidden and the userform appears.
This is long enough for someone to take a screenshot or see valuable information behind the userform.
It also doesn't look very tidy!
Is there a way to alter anything within the VBA to accomplish this?
I have discovered that it is possible with batch scripts or something similar but I have no experience of this and would prefer not to add another dimension to an already complex form.
I'd opt for a Workbook_BeforeClose event that hides all of the sensitive sheets. That way your data remains hidden to people opening your file without macros enabled.
This goes in a new standard module
Option Explicit
Option Private Module
Public Sub SheetsHidden(ByRef hidden As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.name <> "Home" And hidden Then 'your *safe* sheet name
ws.Visible = xlSheetVeryHidden
Else
ws.Visible = xlSheetVisible
End If
Next ws
End Sub
And then you can call it from your ThisWorkbook module
Private Sub Workbook_BeforeClose(Cancel As Boolean)
SheetsHidden True
End Sub
Once you have authenticated the user you can unhide the sheets with the parameter as False.
I would also recommend exploring UserForms, particularly:
With New UserForm1
.Show vbModeless
'do more with your form
End With
By design, opening Excel file without Excel showing up is not possible without external script or tool.
Easier way to hide all sheets is to save the file as .xla(m) (or using ThisWorkbook.IsAddin = True)
Private Sub Workbook_Open()
ThisWorkbook.IsAddin = True ' True by default for .xla(m) Excel Add-In files
Application.Visible = Workbooks.Count
UserForm1.Show vbModeless
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not ThisWorkbook.IsAddin Then ThisWorkbook.IsAddin = True
If Not ThisWorkbook.Saved Then ThisWorkbook.Save
If Workbooks.Count Then Application.Visible = True Else Application.Quit
End Sub
and in the form close event:
Private Sub UserForm_Terminate()
If Workbooks.Count Then ThisWorkbook.Close True Else Application.Quit
End Sub
I'm trying to write a short macro that will prevent the user of an excel workbook from closing the workbook without protecting the first sheet.
The code shows the message box but then proceeds to close the workbook. From my understanding, if the "Cancel" parameter is set to True, the workbook shouldn't close.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets(1).ProtectContents = True Then
Cancel = False
Else
MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook"
Cancel = True
End If
End Sub
I just need the code to display the message box and then not close if the first sheet is not protected.
I could replicate it if I set Application.EnableEvents to False. In the below example I have remembered its state to place it back as was after, however, I'm not sure how it gets to a state of False to begin with.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim BlnEventState as Boolean
BlnEventState = Application.EnableEvents
Application.EnableEvents = True
If Sheets(1).ProtectContents = True Then
Cancel = False
Else
MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook"
Cancel = True
End If
Application.EnableEvents = BlnEventState
End Sub
It may be a safer long term option to force the state rather then set it back.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EnableEvents = True
If Sheets(1).ProtectContents = True Then
Cancel = False
Else
MsgBox "Please Protect 'Unique Futures' Worksheet Before Closing Workbook"
Cancel = True
End If
End Sub
I want to unshare a excel workbook everyday at 11:00pm.
First I use windows task scheduler to open the file at 10:59:45pm, and then run the following code.
Would the following code work?
Sub Unshare()
Application.DisplayAlerts = False
If ThisWorkbook.MultiUserEditing Then
ThisWorkbook.ExclusiveAccess
Application.DisplayAlerts = True
ThisWorkbook.Close
Else
Application.DisplayAlerts = True
ThisWorkbook.Close
End Sub
Sub Workbook Open()
Application.OnTime TimeValue("23:00:00"), "Unshare"
End Sub
Also, all of the code is located in Thisworkbook.
Thanks!
Try using Workbook_Open event in the private module of the Workbook object.
Private Sub Workbook_Open( )
Application.OnTime TimeValue("23:00:00"), "Unshare"
End Sub