Hide a specific workbook via userform in vba - vba

I have a userform with 2 command buttons : hide and show.
This work if I only have 1 workbook open. I can simply hide and show the workbook from the form. However If I have another workbook open let say Book1. and then I click Hide, It will also hide the Book1. I want only to hide the specific workbook.
Here's my code:
Private Sub cmdHide_Click()
'ThisWorkbook("hide_sheet").
Application.Visible = False
End Sub
Private Sub cmdShow_Click()
'ThisWorkbook("hide_sheet").
Application.Visible = True
End Sub

Should be something like this
Option Explicit
Private Sub cmdHide_Click()
'ThisWorkbook("hide_sheet").
Workbooks("Book1.xlsx").Windows(1).Visible = False
End Sub
Private Sub cmdShow_Click()
'ThisWorkbook("hide_sheet").
Workbooks("Book1.xlsx").Windows(1).Visible = True
End Sub
Another Example that work on both Excel 2010 & 2013
Option Explicit
Private Sub cmdHide_Click()
'ThisWorkbook("hide_sheet").
Windows(ThisWorkbook.Name).Visible = False
End Sub
Private Sub cmdShow_Click()
'ThisWorkbook("hide_sheet").
Windows(ThisWorkbook.Name).Visible = True
End Sub

Related

Limiting user interaction with the workbook - hide workbook / show only userform

Is it possible to get the same effect using ThisWorkbook.Application.Visible = False but only for one Workbook. I mean, I'd like to limiting user interaction only to UserForm, but I need have an access to anothers workbooks. At the moment this function cause hide workbook, but after open some another excel file - all object from userform are not available.
Private Sub Workbook_Open()
ThisWorkbook.Application.Visible = False
Starter.Show modeless
End Sub
Thanks for your support.
Please, create a form, let us say "Starter", having (at least) a button ("btExit"), copy the next code in its code module and show it. If the form in discussion already has some code in Initialize and Terminate events, please add the next code lines, too:
Option Explicit
Private Sub btExit_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
ThisWorkbook.Windows(1).Visible = False
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Windows(1).Visible = True
End Sub
So, you can simple use workbook Open event in this way:
Private Sub Workbook_Open()
Starter.Show vbModeless
End Sub

ActiveX Combobox doesn't close automatically

I have an ActiveX Combobox in one of my main sheet which control/update a series of charts.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
End Sub
I noticed that when I click the ComboBox and select one of its content, it leaves a blue highlight on the selection. So to prevent that, I added:
Private Sub cmBoxSelect_DropButtonClick()
Application.ScreenUpdating = False
ActiveCell.Activate
Application.ScreenUpdating = True
End Sub
It successfully removed the highlight.
However, it has a weird drawback. cmbSelect doesn't close automatically once user didn't select anything (once the combobox is active and the user click any cell in the sheet, it doesn't close out). It was working before I added the DropButtonClick event.
Did I missed anything or any wrong steps above? Thanks for your inputs!
EDIT#1
Seems I already found a solution by trial and error. I only added a blank Label and select it to remove the focus out of the ComboBox whenever there is a change. I also changed the DropButtonClick to LostFocus.
Private Sub cmBoxSelect_GotFocus()
Application.ScreenUpdating = False
With Me.cmBoxSelect
.List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
.ListRows = 6
.DropDown
End With
Application.ScreenUpdating = True
End Sub
Private Sub cmBoxSelect_LostFocus()
ActiveCell.Select
End Sub
Private Sub cmBoxSelect_Change()
'series of codes which manipulates the charts, based on selection...
Me.Label1.Select
End Sub
You need to put the SelLength to 0 in multiple events to avoid highlighting:
so:
Me.cmBoxSelect.SelLength = 0
in:
Private Sub cmBoxSelect_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Private Sub cmBoxSelect_LostFocus()
Private Sub cmBoxSelect_DropButtonClick()
Private Sub cmBoxSelect_Change()
Private Sub cmBoxSelect_GotFocus()
(you could add also Me.cmBoxSelect.SelStart = 0 )
Lets try this:
Not Event-triggered by a change, but by the dropbuttonclick
Private Sub changingComboBox(String s)
'series of codes which manipulates the charts, based on selection...
End Sub
Private Sub cmBoxSelect_DropButtonClick()
Dim s As String
s = cmBoxSelect.SelText
If (cmBoxSelect.SelText = cmBoxSelect.Value) Then
cmBoxSelect.Value = ""
cmBoxSelect.Value = s
Else
call changingComboBox(cmBoxSelect.Value)
End If
End Sub
How about that ?

starting a userform without excel in the background

I have this vba code where it has a userform.
Now I would like to start the userform; where the people can't see the Worksheets.
I have added this in my code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Application.Visible = True
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True End Sub
Then in the "ThisWorkbook; I have added this:
Private Sub Workbook_Open()
Application.Visible = False ' only for final version
UserForm1.Show
'enter code here
End Sub
The thing is when I upload this .xlsm to a website, and when you open it; it does not start the userform right away. It somehow goes to an error. If you have an excel open.
Not sure how to protect the file, without having the endusers altering the file
Any ideas?
Maybe this helps:
In "ThisWorkbook" under "Open" add:
Private Sub Workbook_Open()
Application.Visible = False
UserForm2.Show vbModeless
End Sub
This will hide Excel and execute UserForm2.
BTW: To end your program add this code (e.g. to one of your Userform Buttons):
Private Sub CommandButton1_Click()
Application.Quit
End Sub
It will close Excel.

Close workbook with Userform

I have made a User form in Excel, so when i open the xlsm-file it only opens the user form, and the workbook is hidden.
But when i close the user form with the [X]-button I want it to close both the workbook and the user form without saving.
When I close the user form now, and try to open the same file again, it says that it is already/stil open.
Start up code:
Private Sub Workbook_Open()
Application.Visible = False
Fordelinger.Show vbModeless
End Sub
Close code:
Private Sub Fordelinger_Deactivate()
Application.Quit
Workbooks("EstimatDOK.xlsm").Close True
End Sub
Can anyone help? :)
may be you wanted this code in the UserForm code pane
Private Sub UserForm_Terminate()
ThisWorkbook.Close
End Sub
you can use the below code to restrict close (X) button
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then
Cancel = True
MsgBox "The X is disabled, please use a button on the form to Exit.", vbCritical
End If
End Sub
or
Private Sub UserForm_Terminate()
ThisWorkbook.Close savechanges:=False
Application.Quit
End Sub

Remove shadow from ActiveX Option Button in Excel 2010 VBA

I was trying to emphasis the option button from activeX control when it is selected by user. I decided to show the shadow when it is selected and then hide the shadow when user selects other option button. The first process is working whereas the shadow cannot be removed even though I select other button. My VBA code is shown below:
Private Sub OptionButton1_Click()
OptionButton1.Shadow = False
If OptionButton1.Value = True Then
OptionButton1.Shadow = True
Else
OptionButton1.Shadow = False
End If
End Sub
Can anyone please help me to solve this?
In case of FORMS buttons you can use
Sub RemoveFormsButtonShadows()
'A.Leine 21/10/2015
For Each Button In ActiveSheet.Shapes
Button.Select
Selection.ShapeRange.Shadow.Visible = False
Next Button
End Sub
For that you have to create one sub which needs to be called from all the option buttons that you have. This common sub will simply remove the shadow from all option buttons. Here I am taking the example of 3 option buttons.
Option Explicit
Private Sub OptionButton1_Click()
RemoveShadow
If OptionButton1.Value = True Then _
OptionButton1.Shadow = True
End Sub
Private Sub OptionButton2_Click()
RemoveShadow
If OptionButton2.Value = True Then _
OptionButton2.Shadow = True
End Sub
Private Sub OptionButton3_Click()
RemoveShadow
If OptionButton3.Value = True Then _
OptionButton3.Shadow = True
End Sub
Sub RemoveShadow()
Dim objOpt As OLEObject
With ActiveSheet
For Each objOpt In .OLEObjects
If TypeName(objOpt.Object) = "OptionButton" Then
objOpt.Shadow = False
End If
Next
End With
End Sub