Show Excel 2007 Ribbon in XLS file using Excel VBA - 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)"

Related

userForm disabling other workbooks

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)

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

How to hide sheets in your excel file that are not filled in

I have an excel that hides certain sheets when opening the document. So like
Private Sub Workbook_Open()
Worksheets("screen_2").Visible = xlHidden
Worksheets("screen_3").Visible = xlHidden
end sub
Now I have the case that someone fills in either sheet "screen_2" or "screen_3". Right now when someone has filled in either screen_2 of screen_3 sheet closes the document and reopen the excel sheet "screen_2" and "screen_3" are not visible.
What I would like to achieve is that the first time you open the excel document both sheets should be hidden but that when you fill in one of the two sheets that particular sheet should not be hidden when reopening the excel file.
So when I open the excel sheet and fill in screen_2, save, close en reopen the file "screen_2" sheet should be visible (and screen_3 hidden) and when I open the excel doc and fill in screen_3, save, close en reopen the file "screen_3" sheet should be visible (and screen_2 hidden)
Any thoughts on how I can get this working?
once you get through how to fill a hidden sheet, then you could use:
Private Sub Workbook_Open()
CheckIfHideSheet Worksheets("screen_2")
CheckIfHideSheet Worksheets("screen_3")
End Sub
Sub CheckIfHideSheet(sht As Worksheet)
With sht
If .UsedRange.Address = "$A$1" And IsEmpty(.Cells(1, 1)) Then .Visible = xlHidden
End With
End Sub

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!

VBA Excel 2013 form closes when selecting from ComboBox

Can anyone see an issue in this block of code. I had no issues in 2010, but seem to be running into a lot when running VBA in 2013.
I have a control form that is activated from an excel sheet, which has several options, then this form is opened (form those options). The form opens, but when this first combobox selection is activated the form closes and essentially crashes.
Public Sub cmbSelectAccount_DropButtonClick()
Application.ScreenUpdating = False
Workbooks.Open Filename:="C:\Users\xxxxxx\Desktop\New folder\accounts.xlsx"
Sheets("Accounts").Activate
ActiveSheet.Range("a2:a199").Select
Me.cmbSelectAccount.List = Worksheets("Accounts").Range("a2:a199").Value
myAccount = Me.cmbSelectAccount.Value
Workbooks("Accounts").Close
End Sub
If I step through the code it works flawlessly. Any help is greatly appreciated.
Try this (UNTESTED)
Public Sub cmbSelectAccount_DropButtonClick()
Dim wb As Workbook
Application.ScreenUpdating = False
Set wb = Workbooks.Open(Filename:="C:\Users\xxxxxx\Desktop\New folder\accounts.xlsx")
DoEvents
Me.cmbSelectAccount.List = wb.sheets("Accounts").Range("a2:a199").Value
DoEvents
myAccount = Me.cmbSelectAccount.Value
wb.Close (False)
End Sub
there are a few quirks with Sheets vs Worksheets in Excel, and maybe in 2013 they tried to fix them. I'd try changing your 2 references to sheets and worksheets to all possible combinations, starting first with sheets, sheets then worksheets worksheets, then worksheets sheets