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
Related
I TRY THIS CODE BUT IT DOESNT WORK
Range("L3:ED3").Formula = "=IF(C3=0,L3:ED3,"",IF(C3>0,L3:ED3)).clearcontents"
If you want a VBA code to clear the range ("I3:ED3") if cell ("C3") is empty, then you will want to create a Worksheet_Change code. Open Visual Basic Editor (Alt + F11), On the left side under VBAProject in the Project Explorer, Click on Microsoft Excel Objects, and Double Click the sheet that you want this code to work on (If you don't see the window to the left, click Ctrl + r to open the Project Explorer) . In my example I selected "Sheet1" to paste this code in. Once you are in the Worksheet module for the corresponding sheet, paste the code below.
Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Range("C3").Value = "" Then Range("I3:ED3").ClearContents
Application.EnableEvents = True
End Sub
I'm building an Excel add-in which allows users to interact with PowerPoint and export data. The users have an option to insert a template into an existing workbook via a button in the ribbon. When they click this, the button becomes disabled as you can't have two templates in one workbook.
The user may then save their workbook and come back to it at a later point. My issue is that I need to detect that they have the template inserted into an existing workbook and I do this by trying to detect if a named range exists (called LoadedToken).
The issue I have is that both the onLoad function of the Ribbon and the VBA class Auto_Open launch before a large workbook is fully loaded. Therefore I'm not actually detecting if the template exists and can't actually disable the button.
Is there such a thing as events in Excel which can be called at a worksheet level (like worksheet_open) but detected in a class?
My code is:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' When the Ribbon loads
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub OnRibbonLoad(ribbonUI As IRibbonUI)
Set MyRibbonUI = ribbonUI
Dim Authenticate As New AuthenticationClass
On Error Resume Next
' Enable/Disable buttons based on authentication status
If Authenticate.Authentify = False Then
Authenticated = False
Debug.Print ("Unauthorized!")
Else
Authenticated = True
Debug.Print ("Authorized!")
End If
' If the template doesn't exist then disable a grouping on the ribbon
If TemplateExists = False Then
Call RefreshRibbon(Tag:="Grouping1")
Else
Call RefreshRibbon(Tag:="Grouping2")
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Check if the Template sheet is visible and prevent other buttons from working
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function TemplateExists()
Dim Helpers As New HelpersClass
If Helpers.IsNamedRange("LoadedToken") Then
TemplateExists = True
Else
TemplateExists = False
End If
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' From the Helpers Class - Check if a named range exists
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function IsNamedRange(RName As String) As Boolean
Dim N As name
IsNamedRange = False
For Each N In ActiveWorkbook.Names
If N.name = RName Then
IsNamedRange = True
Exit For
End If
Next
End Function
Note: The authentication class is just another class to connect to my API.
I am trying to set up a button in Excel to show a dual view of two separate worksheets at the same time. This is the code I've written so far (see below). So far the code seems to work. The problem is that the top window has some activex controls on the worksheet, and they seem to stop working until the bottom window is closed again. Why is this happening, and what can I do to fix it? Thanks.
Private Sub DualViewButton_Click()
Dim windowToPutOnTimeline As Window
If Windows.Count = 1 Then
ThisWorkbook.NewWindow
Windows.Arrange xlArrangeStyleHorizontal, True, False, False
Set windowToPutOnTimeline = Windows(1)
If Windows(1).Top < Windows(2).Top Then
Set windowToPutOnTimeline = Windows(2)
End If
With windowToPutOnTimeline
.Activate
HorizontalTimelineSheet.Activate
.DisplayGridlines = False
.DisplayRuler = False
.DisplayHeadings = False
.DisplayWorkbookTabs = False
'.EnableResize = False
End With
Windows(2).Activate 'go back to the right focus the user expects.
Else
If Windows(1).Top = Windows(2).Top Then
Windows.Arrange xlArrangeStyleHorizontal, True, False, False
Else
Windows.Arrange xlArrangeStyleVertical, True, False, False
End If
End If
End Sub
EDIT: if I switch the window that's being assigned to windowToPutOnTimeline then the problem goes away. So I've essentially worked around the problem without knowing why it works the other way. (see code snippet below)
With ThisWorkbook
Set windowToPutOnTimeline = .Windows(1)
Set windowToPutOnDataSheet = .Windows(2)
tmp = .Windows(1).Top
.Windows(1).Top = .Windows(2).Top
.Windows(2).Top = tmp
End With
This behavior is a bug in ActiveX control.
As a work around, use a button from the Forms Controls, rather than an ActiveX button
Using the Forms button you will need to add a Module, declare a Sub with your code and assign the Sub as the action macro to your button (as apposed to putting your code in the click event of an ActiveX button)
I tried this on Excel 2007, seem to work OK - the button appears and works on both windows
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)"
I used this vba code in the ThisWorkbook module to disable the right click menu in an Excel workbook.
Private Sub Workbook_Activate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = False
End With
End Sub
Private Sub Workbook_Deactivate()
With Application.CommandBars.FindControl(ID:=847)
.Visible = True
End With
End Sub
Works like a charm.
Problem is, I can't access the right click menu on tabs in ANY workbook now.
The second part of the code is supposed to turn it back on, I assumed? Yet it doesn't.
Even when I remove the code entirely, no workbook, not even a new one, has a menu when I click right on one of the tabs.
Is there a general vba codesnippet that "resets" excel maybe? Or a general "enable all menus" thing?
REVISION:
This code posted here doesn't disable the rightclick menu, it removes the "delete" option from that specific menu.
omg
Application.CommandBars("Ply").Enabled = True
-.-
Started googling different keywords after the last edit and BAM.
Late again as usual, but tackled with the same problem today. Here's the solution to get your right-click functionality back:
Option Explicit
'
Sub tester()
'
Dim cBar As CommandBar
'
For Each cBar In CommandBars
Debug.Print cBar.Name
If (cBar.Type = msoBarTypePopup) Then cBar.Enabled = True
Next
End Sub
Also note that the below also exist. Some macro from work had them all disabled in my Excel.
Application.CommandBars("Cell").Enabled = True
Application.CommandBars("Row").Enabled = True
Application.CommandBars("Column").Enabled = True