userForm disabling other workbooks - vba

Does anyone know how to enable editing on other workbooks while UserForm is opened? Below makro that I've put into workbook
Private Sub Workbook_Open()
If Environ("USERNAME") <> "name.name" Then
Worksheets("EQUIPMENT").Visible = xlVeryHidden
Worksheets("EQUIPMENTFULL").Visible = xlVeryHidden
Worksheets("SITES").Visible = xlVeryHidden
Worksheets("EMPLOYEES").Visible = xlVeryHidden
Worksheets("NEW").Visible = True
Else: For i = 1 To Worksheets.Count
Worksheets(i).Visible = True
Next i
End If
UserForm.Show
End Sub

It is possible to create modeless (as well as modal) dialog boxes. Modeless dialog boxes allow the user to continue to work in the application while the form is still displayed.
By default all userforms are displayed as modal which means that the user must close the userform before they can contine to use the application. When a UserForm is modal, the user must supply information or close the userform before using any other part of the application. No subsequent code is executed until the userform is hidden or unloaded.
Although other forms in the application are disabled when a userform is displayed, other applications are not.
When the UserForm is modeless, the user can view other forms or windows without closing the UserForm.
Userform1.Show ([modal])
with:
VBA.FormShowConstants.vbModal = 1
VBA.FormShowConstants.vbModeless = 0
(Taken verbatim from https://bettersolutions.com/vba/userforms/modeless.htm)

Related

Disable checkboxes when sheet opened in vba

I have 3 checkboxes. When a user opened my sheet, he/she must not check checkboxes. I want them to be disabled. How can I do that?
Not sure if you meant ActiveX or FormControl, so here you go
Code
Private Sub Worksheet_Activate()
Dim myActiveX As Object
Set myActiveX = Sheet1.OLEObjects("CheckBox1")
myActiveX.Object.Value = True
myActiveX.Object.Locked = False ' Make it False if you wish to enable it
myActiveX.Object.Enabled = False ' Another option to disable
Dim myFormControl As CheckBox
Set myFormControl = ActiveSheet.Shapes("Check Box 1").OLEFormat.Object
myFormControl.Value = True
myFormControl.Enabled = False ' Make it True if you wish to enable it
End Sub
Live GIF demo
You have to write some VBA code in order to do that.
Let's suppose that you have 3 CheckBoxes in your first sheet.
By holding the "Alt" key on your keyboard and the pressing one time the "F11" key, opens Microsoft Visual Basic. ( Alt + F11 )
At your left hand you can see the "VBAProject" tree.
Double click on the "ThisWorkbook" file and copy the following code in the window it will appear:
Private Sub Workbook_Open()
void = uncheckAllCheckboxes()
End Sub
Function uncheckAllCheckboxes()
ThisWorkbook.Worksheets(1).CheckBox1.Value = False
ThisWorkbook.Worksheets(1).CheckBox2.Value = False
ThisWorkbook.Worksheets(1).CheckBox3.Value = False
End Function
Save the excel file as "Excel 97-2003 Workbook" type (.xls)
Close your excel.
Open the file you have previously saved and everything will work fine.
;)
P.S.: It is important to enable macros from your excel settings

Prevent VBA code from other workbook working with objects

I need to disable a control (list box in this case), so user cannot get data from other departments. I do have a macro that can enable/disable it, and ask for password.
What I need is to prevent semidecent user to write a macro for enabling this list box in another workbook and unlocking it this way. It is possible in some way prevent VBA code from other modules to work with this control? So only code written in that one sheet can enable or disable it.
Thank you
What you can do is lock your VBA project and change your "subs" into "function", this way, a more advanced user have no access to the macro you've written without unlocking the project.
(Functions are not visible from the "assign a macro" button in the developer ribbon where Subs are)
I tried UserInterFaceOnly - and I'm not sure at this moment what it is supposed to do but it is not doing what I wanted
To be more clear, I have 2 list boxes and several comboboxes on Sheet1. When I'm sending file to users, I want to lock ListBoxes, or rather .enabled = False
What I'm trying to do is to prevent other user to write in some other workbook
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True
this line woudl enable the list box, and user can change values, and macro will genarate new SQL string for Departments user is not supposed to have access to. I need to have comboboxes enabled, so user can view different dates, and versions of plan for his department.
By accident I found a way I guess.
Within Sheet1 I have this code
Sub lstDepartment_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
Call ControlsM.directorpopulation
End If
End Sub
Sub lstDirector_change()
'checks first if lock checkbox is true/false, clears selection and disables listbox, _
if checkbox is locked and yet listbox is enabled and allows change
If Me.chkLock = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstdirector").Enabled = False
Call clearlistbox
End
ElseIf Me.chkLock = False Then
End If
End Sub
Private Sub chkLock_Click()
'locking/unlocking with password inbox call
If Me.chkLock.Value = True Then
Me.ListBoxes("lstDepartment").Enabled = False
Me.ListBoxes("lstDirector").Enabled = False
ElseIf Me.chkLock.Value = False Then
If PasswordInputBoxM.InputBoxPassword("Enter password", "Password Required") = "****" Then
Me.ListBoxes("lstDepartment").Enabled = True
Me.ListBoxes("lstDirector").Enabled = True
Else
Me.chkLock.Value = True
MsgBox "Wrong Password"
Exit Sub
End If
End If
End Sub
If someone tries to unlock just listbox, and tries to change value, it will check the checkbox value, and clears the selected list and locks the listbox
If someone tries to unlock listbox and uncheck lock checkbox, it will automatically ask for password
Problem is, that someone can change selected value from other workbook. I do not know how to handle that
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Enabled = True 'handled
Workbooks("opexRequest").Sheets("Report").chkLock.value = False 'handled
Workbooks("opexRequest").Sheets("Report").ListBoxes("lstDepartment").Selected(1) = True 'do not know how to prevent this

Excel 2013 - issue when closing multiple workbooks if one workbook is hidden

I've written a program in Excel VBA which uses a UserForm to enter data by the users. Specifically, it is a Telemarketing Tracker tool: the user fills in the details of the call in a text box on the UserForm and then clicks the relevant button to indicate whether it was a good or bad call, and can then continue with the next call.
This data is stored on a worksheet and our users often prefer to hide the workbook and just view the UserForm. I have developed a couple of methods of hiding the workbook. If there is just one workbook open, I hide the Excel Application. If there is more than one workbook open, I just hide the window. Here is the code I use for this:
Private Sub HideUnhideButton_Click() 'User clicks Hide/Unhide button
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = Not Windows(ThisWorkbook.Name).Visible
HideUnhideButton.Tag = Windows(ThisWorkbook.Name).Visible
Else
ThisWorkbook.Application.Visible = Not ThisWorkbook.Application.Visible
HideUnhideButton.Tag = ThisWorkbook.Application.Visible
End If
ThisWorkbook.Activate
End Sub
This works well but obviously certain issues arise when the user has the workbook hidden and then open a different Excel Workbook. I've worked round most of these issues, but there is one thing I can't seem to work out: if the Telemarketing workbook is hidden, with another workbook open, if I click the Close button, both workbooks try to close.
I've tried creating a class module with an Application Level event tracker so that all workbooks' close events are monitored. But my problem is that when I click the close button, the first workbook that tries to close is the hidden workbook. So I can catch the close event and prevent the hidden workbook from closing but if I set Cancel to True, it prevents all the workbooks from closing!
The only workaround I can think of is when the user tries to close a workbook, I cancel the Close Event and Unhide the hidden workbook. But I don't know how to identify which workbook the user was attempting to close - so I can't work out how to automatically close the correct workbook.
I have currently set up the WorkbookBeforeClose event as follows:
Public WithEvents A As Excel.Application
Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
If Workbooks.Count > 1 Then
If Windows(ThisWorkbook.Name).Visible = False Then
If Wb.Name = ThisWorkbook.Name Then
Cancel = True
End If
End If
End If
End Sub
If I step through this code, I find that the Wb.Name is the name of the Telemarketing Workbook (even though it's hidden) and the name of the workbook that the user is actually trying to close does not appear at all - as far as I can work out.
Can anyone make any further suggestions?
The other thing I should mention is that it needs to work over Excel 2013 and Excel 2010.
I'm sorry to post an answer to my own question so quickly. It sort of indicates I didn't do quite enough research beforehand. However, for anyone who has a similar problem, here's my solution. This code needs to be posted in a class module and an instance of the class needs to be created before it will work, of course.
Note: in the below example, "TT" relates to the Telemarketing Tracker
Public WithEvents A As Excel.Application
Private Sub A_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Dim VIS As Boolean, myAW As Workbook
If Workbooks.Count > 1 Then 'if there is more than one workbook open...
If Windows(ThisWorkbook.Name).Visible = False Then 'and if TT is invisible...
If ActiveWorkbook.Name = ThisWorkbook.Name Then 'and if the workbook being closed is the TT.
Windows(ThisWorkbook.Name).Visible = True
Else 'more than one wb open, TT is invisible, and the workbook being closed is NOT the TT.
Set myAW = ActiveWorkbook
Cancel = True
Windows(ThisWorkbook.Name).Visible = True
Application.EnableEvents = False
myAW.Close
Application.EnableEvents = True
If TelesalesForm.HideUnhideButton.Tag = "False" Then 'NB: I use a tag on the Hide/Unhide button on the UserForm to store whether the workbook should be hidden or not.
If Workbooks.Count > 1 Then
Windows(ThisWorkbook.Name).Visible = False
Else
ThisWorkbook.Application.Visible = False
End If
End If
Exit Sub
End If
ElseIf ActiveWorkbook.Name <> ThisWorkbook.Name Then
'more than one workbook open and the TT is visible and the workbook being closed is NOT the TT
Exit Sub
End If
End If
'code gets to this point ONLY under the following circumstances:
'There is only one workbook open (i.e. the TT) OR
'There is more than one WB open and the WB being closed is the TT.
'The rest of the code goes here for managing the closing of the TT.
End Sub
If anyone thinks of any other embellishments or improvements to the code then I would be very glad to hear of them!

Hide commandbutton to print document via a created button

This is my first post on here, I will try to be as clear as possible :)
I'm creating a Microsoft Word form for users to fill in, this form is protected and only the forms can be filled in the rest of the document is protected with the password: "mypass"
I want to have a button on the document it self wich prints the active document. What I did is create a print button into a “drawing” Textbox ( Insert | Textbox ) as stated here.
This Print button must be hidden so it's not visible on the document when it's printed.
Here is the code:
Private Sub CommandButton1_Click()
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:="mypass"
End If
With ActiveDocument
.Shapes(1).Visible = msoFalse
.PrintOut Copies:=1
.Shapes(1).Visible = msoTrue
End With
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, Password:="mypass"
End Sub
When I click the print button in protective mode nothing happens, when I turn off protective mode I get this error:
Error 4641 at run.
ToolsProtectDocument statement is currently disabled
To summarize:
The Print button I created does not work when protective mode is enabled.
When protective mode is disabled I get the error that the ToolsProtectDocument statement is currently disabled.
I want to have a procted form that can be filled in and be printed from the form itself without the print button being visable on the printed form.
Does anyone have a clue?

Show Excel 2007 Ribbon in XLS file using Excel VBA

I have an excel dashboard which works such that before the excel file is closed, I would like to display all the EXCEL ribbon, so that next time excel is opened, the application / excel will show the ribbon. At present, it does not show the ribbon if excel is opened.
Private Sub Workbook_BeforeClose(cancel As Boolean)
On Error Resume Next
Application.ScreenUpdating = True
ActiveWindow.DisplayWorkbookTabs = True
Application.DisplayFormulaBar = True
Application.DisplayFullScreen = False
Application.DisplayStatusBar = True
Application.DisplayScrollBars = True
Application.ScreenUpdating = True
Sheets("Introduction").Select
End Sub
This is an .xls file with Macro and supposed to work in Excel 2003 and Excel 2007.
Also, if "Cancel" is clicked, I do not want to show any of the above / ribbon, as user is supposed to get a protected view of the excel dashboard.
If the ribbon is closed by default, you can open it again by double clicking on one of the tabs (for instance, the Home tab).
(See this for more details).
If, however, you wish to write an event to take place when the workbook opens, then use the Workbook_Open() event from the ThisWorkbook Excel Object.
try this Application.ExecuteExcel4Macro " show.toolbar(""Ribbon"",true)"
to hide
Application.ExecuteExcel4Macro " show.toolbar(""Ribbon"",false)"