I am not sure, even if this is possible. I would like to change the named of the button when clicked.
I have Active X button that will enable cells on the sheet when clicked. Name of the button "Enable Sheet".
Once he enables, he should see a button "Disable Sheet". Please advise.
assuming your button is named after "CommandButton1":
Private Sub CommandButton1_Click()
With Me.OLEObjects("CommandButton1").Object
.Caption = IIf(.Caption = "Enable Sheet", "Disable Sheet", "Enable Sheet")
End With
End Sub
edited after OP's comments
Private Sub CommandButton1_Click()
With Me.OLEObjects("CommandButton1").Object
.Caption = IIf(.Caption = "Enable", "Disable", "Enable")
Range("E13:E14").Locked = .Caption = "Enable"
End With
End Sub
In the code that you can attach to the ActiveX object, you can try something like this:
Private Sub CommandButton1_Click()
Me.CommandButton1.Caption = "Enable"
End Sub
In general, for ActiveX Objects, you can change the displayed text using the .Caption property.
Related
I created a user form with multiple options and now I want that the option the user selects is shown in a label under the button that calls the user form.I changed the caption in the text box under the button to resemble what should happen
However my options aren't working. Should I save the output in a global variable and then call it back to change the label and if so how do I do that? Or is it possible to just call the selection within the user form?
The code I was trying to run was this one to call the message box and then change the text box which is actually a label called "labelpage"
Private Sub CommandButton1_Click()
UserForm1.Show
If UserForm1.OptionButton1 = True Then LabelPage.Caption = "Company Restricted"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Strictly Confidential"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Public Information (does not need to be marked)"
End Sub
I also had this for each button click just to close them after selection, within the user form code.
Private Sub OptionButton1_Click()
OptionButton1.Value = True
Unload Me
End Sub
Private Sub OptionButton2_Click()
OptionButton2.Value = True
Unload Me
End Sub
Private Sub OptionButton3_Click()
OptionButton3.Value = True
Unload Me
End Sub
Is there just a tiny mistake of syntax or something like that or is this just completely wrong? Thank you in advance for your help.
The issue is that you are unloading the UserForm, meaning the controls are not available to you. The solution is to just hide the UserForm:
Private Sub OptionButton1_Click()
Hide
End Sub
Private Sub OptionButton2_Click()
Hide
End Sub
Private Sub OptionButton3_Click()
Hide
End Sub
how I can check if a checkbox is selected ?
And after selecting a checkbox for example "a" (in my example) I want to open a excel sheet.
How can I solve this problem?
Thank you all.
In the UserForm module you can just place the following code, when your CheckBox is named "CheckBox1":
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
Worksheets("Sheet1").Visible = True
Else
Worksheets("Sheet1").Visible = False
End If
End Sub
This will make "Sheet1" visible when it's checked and invisible when unchecked.
If you name your checkbox differently, you'll see that if you double click the checkbox in the Userform Design, the VBE will already come up with
Private Sub CheckBoxName_Click()
End Sub
My question at hand is why do these Macros for creating Menu Command buttons work on my personal computer (Excel 2010/2016), but generate macro may not be available messages on my work one. My only thoughts are it's relative to the bit /configuration of my work laptop, some various complier issue tied to old VBA libraries, assignment references being missed, & something random related to private Subs.
Macros in This Workbook. The coding is done in This Workbook, Module, & User Form (Command Button) . The VBA essentially programs a new button on the Excel Ribbon (Menu Commands) that when you click it an user form will generate. However, this does not happen with the VBA error. In overall, the coding is very sound. Therefore, I believe the errors I'm getting are tied to system/configuration items.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
RemoveMenuItem
End Sub
Private Sub Workbook_Open()
AddMenuItem
'frmMain.Show
End Sub
MODULE
Option Explicit
Sub Selectfile()
Dim wb As Workbook
Set wb = Workbooks.Open("C:\Users\brian\Desktop\practice file2.xlsm")
End Sub
Sub AddMenuItem()
Dim cmbWorkSheetMenuBar As CommandBar
Dim cmbControl As CommandBarControl
Set cmbWorkSheetMenuBar = Application.CommandBars("Worksheet Menu Bar")
Set cmbControl = cmbWorkSheetMenuBar.Controls.Add(Type:=msoControlButton, temporary:=True) 'adds a menu item to the Menu Bar
With cmbControl
.Caption = "Show Form" 'names the menu item
.OnAction = "ShowTheForm" 'runs the specified macro
.FaceId = 1098 'assigns an icon to the dropdown
End With
End Sub
Sub RemoveMenuItem()
On Error Resume Next 'in case the menu item has already been deleted
Application.CommandBars("Worksheet Menu Bar").Controls("Show Form").Delete 'delete the menu item
End Sub
Sub ShowTheForm()
frmMain.Show
End Sub
UserForm Click Code
Option Explicit
Private Sub btnOne_Click()
Application.FindFile
End Sub
I have a userform with a multiple frames, all filled with multiple checkboxes. I've named the checkboxes to their corresponding Excel cells. Now I want to make VBA run on clicking any of these checkboxes on run time. I know I can do this by creating a click-sub for every individual checkbox, but there must be a cleaner way to do this.
So far I've tried to put this code in the userform_Click and userform_Mousedown events, but they don't run when I click the checkboxes. Does anyone have an idea how to do this?
Dim iControl As Control
For Each iControl In Me.Controls
If TypeName(iControl) = "CheckBox" Then
If iControl.Value = True And Range(iControl.Name).Value = "" Then
Range(iControl.Name).Value = Format(Now, "dd.mm.yyyy")
ElseIf iControl.Value = True And Range(iControl.Name).Font.Color = vbWhite Then
Range(iControl.Name).Font.Color = vbBlack
ElseIf iControl.Value = False And Range(iControl.Name).Value <> "" Then
Range(iControl.Name).Font.Color = vbWhite
End If
End If
Next
As SilentRevolution said - you need an event to fire when you click the button. You're just after a single procedure to fire all check box click events.
So:
Create a class module called cls_ChkBox.
In the class module you'll add the Click event code:
Option Explicit
Private WithEvents chkBox As MSForms.CheckBox
Public Sub AssignClicks(ctrl As Control)
Set chkBox = ctrl
End Sub
Private Sub chkBox_Click()
ThisWorkbook.Worksheets("Sheet1").Range(chkBox.Name).Value = Format(Now, "dd.mm.yyyy")
End Sub
Now you just need to attach the chkBox_Click event to each check box on your form. In your user form add this code:
Option Explicit
Private colTickBoxes As Collection
Private Sub UserForm_Initialize()
Dim ChkBoxes As cls_ChkBox
Dim ctrl As Control
Set colTickBoxes = New Collection
For Each ctrl In Me.Controls
If TypeName(ctrl) = "CheckBox" Then
Set ChkBoxes = New cls_ChkBox
ChkBoxes.AssignClicks ctrl
colTickBoxes.Add ChkBoxes
End If
Next ctrl
End Sub
Each check box is given its own instance of the class, which is stored in the colTickBoxes collection.
Open the form and the cell in Sheet1 will update to show the date depending on the name of the check box.
You need an event to run code, if there is no event, the macro cannot start. I don't know if there is a single event that triggers for any button or checkbox that is clicked.
If the code you want to execute is the same every time except the control, you could write a private sub in the userform module which is called for each event, the private sub can take an input between the () for example.
Private Sub CheckBox1_Click()
Call ExecuteCode(Me.CheckBox1)
End Sub
Private Sub CheckBox2_Click()
Call ExecuteCode(Me.CheckBox2)
End Sub
Private Sub CheckBox3_Click()
Call ExecuteCode(Me.CheckBox2)
End Sub
Private Sub ExecuteCode(IControl As Control)
If TypeName(IControl) = "CheckBox" Then
If IControl.Value = True And Range(IControl.Name).Value = "" Then
Range(IControl.Name).Value = Format(Now, "dd.mm.yyyy")
ElseIf IControl.Value = True And Range(IControl.Name).Font.Color = vbWhite Then
Range(IControl.Name).Font.Color = vbBlack
ElseIf IControl.Value = False And Range(IControl.Name).Value <> "" Then
Range(IControl.Name).Font.Color = vbWhite
End If
End If
End Sub
I just learned this today and thought it might help? Most of the questions, and responses for that matter, to questions regarding checkboxes, listboxes, etc. seem to not distinguish between those forms are inserted directly on the worksheet or are imbedded in a UserForm.
This may work for a checkbox - it works for a ListBox on a UserForm. If you want code to run after a selection, the code you must write has to be in module of the UserForm. I had no clue how to access. Once you have inserted a UserForm in your VBE, add the ListBoxes or whatever to the UserForm. Right click on the UserForm in the upper left project window and select "view code." Here is where you'll place the code, in my case a "change event" such that after a selection is made from the ListBox, other code is automatically run. Her for example:
Sub lstBoxDates_Change()
Dim inputString1 As String
inputString1 = Format(UserForm1.lstBoxDates.Value, "mm-dd-yyyy")
Call EnterDates(inputString1)
Unload Me
End Sub
To explain: Again this code is in the UserForm Module. I named my ListBox,
lstBoxDates. In my main code that I call - EnterDates, I use the variable name = inputString1. The value or date that I have selected from the ListBox is captured from the UserForm1 by UserForm1.lstBoxDates.Value - and I format that to a date, otherwise you see just a number. This is for only one selection.
Because I Dim here, no need to Dim in your main code. The sub for the main code
needs to accept the variable you are passing to it:
Sub EnterDates(inputString1)
This is very generalized but maybe something will click so you can get what you are after. I hope so, for I've worked on this a full two days!!
When some words is selected in the document and right click, macro function should be called. Or something like, custom right click menu option should be added and when I click that menu option, macro should be called.
For example:
In the document I have four digit numbers like 2001 and 2010. When I select four digit number and hit right click and press my custom menu option, macro should be called and selected four digit no. should be parsed to the macro function which is called.
I got some code but its for Excel, I dont't know how to customize it for Microsoft Word.
Code for excel:
Const strMacro = "YourCode"
Sub CreateMacro()
Dim cBut
Call KillMacro
Set cBut = Application.CommandBars("Cell").Controls.Add(Temporary:=True)
With cBut
.Caption = strMacro
.Style = msoButtonCaption
.OnAction = "Test_Macro"
End With
End Sub
Sub Test_Macro()
MsgBox "I work"
End Sub
Sub KillMacro()
On Error Resume Next
Application.CommandBars("Cell").Controls(strMacro).Delete
End Sub
For Microsoft Word 2007, I have pasted the macro code to add a custom menu option in the right click mouse event and getting the selected text into the function.
Macro:
Option Explicit
Sub CreateMacro()
Dim MenuButton As CommandBarButton
With CommandBars("Text")
Set MenuButton = .Controls.Add(msoControlButton)
With MenuButton
.Caption = "Press Me"
.Style = msoButtonCaption
.OnAction = "Test_Macro"
End With
End With
End Sub
Sub Test_Macro()
MsgBox Selection.text
End Sub
Sub ResetRightClick()
Application.CommandBars("Text").Reset
End Sub