Heres the Code that I put in the module1 from Tim Williams
Sub Tester()
Dim isOn As Boolean
With ActiveSheet
Application.Caller = MuddyBoots
isOn = (.CheckBoxes(Application.Caller).Value = xlOn)
.CheckBoxes("TabletUser").Visible = isOn
.CheckBoxes("WebUser").Visible = isOn
End With
End Sub
I have three checkboxes:
MuddyBoots
TabletUser
WebUser
When MuddyBoots is ticked I want TabletUser and WebUser to be visible and when MuddyBoots is unticked I want the two checkboxes TabletUser and WebUser to be invisible.
The code that works-ish is below:
Public Sub TestCheckbox()
Dim s As Shape
Set s = ActiveSheet.Shapes("MuddyBoots")
s.Select
If Selection.Value = xlOn Then
MsgBox "Checked"
ActiveSheet.CheckBoxes("TabletUser").Visible = True
ActiveSheet.CheckBoxes("WebUser").Visible = True
'code here
Else
MsgBox "Not checked"
ActiveSheet.CheckBoxes("WebUser").Visible = False
ActiveSheet.CheckBoxes("TabletUser").Visible = False
'code here
End If
End Sub
This removes the two checkboxes if MuddyBoots is unticked after you ok the pop up message. If I comment out the msgbox line it doesn't work. Then I have to restart the VBA code.
These are form controls.
I also get the error message: Cannot run the macro New User Form Macro'!CheckBox17_Click'. The macro may not be available in this workbook or all macros may be disabled. I have changed the name of the checkboxs in the name box and have triple checked the checkboxes and they have the correct names...
I want to know how to get this functionality working without the popup messages and without the error please.
Sub Tester()
Dim isOn As Boolean
With ActiveSheet
'Application.Caller = name of calling shape
isOn = (.CheckBoxes(Application.Caller).Value = xlOn)
.CheckBoxes("TabletUser").Visible = isOn
.CheckBoxes("WebUser").Visible = isOn
End With
End Sub
Related
I have created a check box in excel and I would like that when I click on the check box name be assigned to a cell in another sheet.
how can i do that?
Here is what I tried but I get an error 424
Private Sub checkbox1_click()
Worksheets("Produits").Range("A1") = checkbox1.Caption
End Sub
Ps: I use Office2019
For the sake of simplicity I will assume that your checkbox is located in a worksheet named Test . I will also assume that when the checkbox is unchecked the corresponding cell should be emptied.
For an ActiveX type of checkbox, named CheckBox1, your code should be located in the same worksheet as the checkbox and should look like so:
Option Explicit
Sub CheckBox1_Click()
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets("Produits")
If CheckBox1.Value Then
sht.Range("A1") = CheckBox1.Caption
Else
sht.Range("A1").ClearContents
End If
End Sub
For a Form Control type of checkbox, named Check Box 2, your code should be located in a module and should look like so:
Option Explicit
Sub CheckBox2_Click()
Dim cb As CheckBox
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set sht1 = ThisWorkbook.Worksheets("Test")
Set sht2 = ThisWorkbook.Worksheets("Produits")
Set cb = sht1.CheckBoxes("Check Box 2") 'whatever the name of your checkbox is...
If cb.Value = 1 Then
sht2.Range("A2") = cb.Caption
Else
sht2.Range("A2").ClearContents
End If
End Sub
You forgot about .Value
Try something like that:
ActiveWorkbook.Sheets("YourSheet").Range("A1").Value = CheckBox1.Caption
But I don't know what's the point about your case. Maybe you'll fined that way more helpful.
If CheckBox1.Value = True Then
ActiveWorkbook.Sheets("YourSheet").Range("A1").Value = CheckBox1.Caption
End If
If CheckBox1.Value = False Then
ActiveWorkbook.Sheets("YourSheet").Range("A1").ClearContents
End If
I want to make a sub that inserts a picture if checkbox is checked and deletes it if it's unchecked, this is what i've got so far, and the first part (insert when checked) works fine. Any ideas, please?
Dim chbx
Set chbx = ActiveSheet.CheckBoxes.Add(240, 15, 144, 15.75)
chbx.Characters.Text = "DisplacementPicturesIns"
chbx.OnAction = "DisplacementPicturesIns"
If chbx.Value = True Then
chbx.OnAction = True
Elseif chbx.Value = False Then
....
End If
I dunno if you still need this but here is a solution that i made to select/unselect my checkboxes elements.
you can change between the if statement to do what ever you want :)
Sub SelectAll()
Dim chkbx As CheckBox
Dim cbv As Long
cbv = Sheets("Feuil1").CheckBoxes(Application.Caller).Value
If cbv = 1 Then
For Each chkbx In Sheets("Feuil1").CheckBoxes
chkbx.Value = xlOn
Next
Else
For Each chkbx In Sheets("Feuil1").CheckBoxes
chkbx.Value = xlOff
Next
End If
End Sub
Like it was pointed out in the notes, your code doesnt make much sense.
the OnAction holds the name of the method to be called once the checkbox is ticked/unticked.
Also, your condition is only being tested once, after the creation of the Checkbox control and thats it. if you want your condition to be tested every time, it must be placed inside the sub that is called on the ticking/unticking event.
My suggestion: give the checkbox a name, and then refer to it inside the sub and check it's value:
Public Sub Example()
With ActiveSheet.CheckBoxes.Add(240, 15, 144, 15.75)
.Characters.Text = "DisplacementPicturesIns"
.Name = "myCheckBox"
.OnAction = "myCheckBox_Click"
End With
End Sub
Sub myCheckBox_Click()
Dim chbx
Set chbx = ActiveSheet.CheckBoxes("myCheckBox")
If chbx.Value = 1 Then
DisplacementPicturesIns
Else
'do other stuff
End If
End Sub
I am currently working on a userform to create an order for users at a company to send to my dept.
At the moment i have come to a standstill as i am struggling to work out the following.
I have a combobox which has a list of products our business offers. Based on the selection i want to be able to add labels and textbox which require the user to enter data for example.
If this selection in the combo box then
Enter name, date required, location of user etc.
Also this needs to be specific to the combobox selection.
Any help would be much appreciated :)
UPDATE
Apologies, as i did not have any code for that function I did not add any Here is the code i have.
Private Sub CommandButton1_Click()
Windows("RFS User Form Mock.xlsm").Visible = True
End Sub
Private Sub LegendDefinition_Change()
LegendDefinition.Locked = True
End Sub
Private Sub RequestList_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Definition").Cells(i, "A").Value = (Me.RequestList) Then
Me.DefinitionBox = Sheets("Definition").Cells(i, "B").Value
End If
Next
End Sub
Private Sub RequestList_DropButtonClick()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
If Me.RequestList.ListCount = 0 Then
For i = 2 To LastRow
Me.RequestList.AddItem Sheets("Definition").Cells(i, "A").Value
Next i
End If
End Sub
Sub UserForm_Initialize()
SiteList.List = Array("Birmingham", "Bristol", "Cardiff", "Chelmsford", "Edinburgh", "Fenchurch Street", "Glasgow", "Guernsey", "Halifax", "Homeworker", "Horsham", "Ipswich", "Jersey", "Leeds", "Leicester", "Lennox Wood", "Liverpool", "Manchester", "Peterborough", "Redhill", "Sunderland", "Madrid")
End Sub
Private Sub VLookUp_Change()
VLookUp.Locked = True
End Sub
When posting a question, you are expected to provide some code showing where you're standing trying to address the problem. Here's nevertheless a short demo that will give you a starting point.
Create a new UserForm and put a combobox, a label and a textbox on it; ensure they're named ComboBox1, Label1 and TextBox1, respectively.
Then, paste this code in the form's module:
Option Explicit
Private Sub ComboBox1_Change()
Dim bVisible As Boolean
'Only show the label and the textbox when the combo list index is 1, which corresponds to "Item 2".
'Note: bVisible = (ComboBox1.Text = "Item 2") would also work.
bVisible = (ComboBox1.ListIndex = 1)
Label1.Visible = bVisible
TextBox1.Visible = bVisible
End Sub
Private Sub UserForm_Layout()
'Populate the combo.
ComboBox1.AddItem "Item 1", 0
ComboBox1.AddItem "Item 2", 1
'Note: the code below could be removed by setting the corresponding
'design-time properties from the form designer.
ComboBox1.Style = fmStyleDropDownList
Label1.Visible = False
TextBox1.Visible = False
End Sub
Then press F5 to show the form. You'll notice that the label and textbox are only visible when the combo shows "Item 2". The visibility adjustment is performed within the ComboBox1_Change event handler.
If you plan on having numerous controls shown / hidden depending on your combo's value, you could group them into one or more Frame controls, and show / hide those frames instead.
I'm having this problem for the last few hours and I would really appreciate some help with it.
Basically, I want to be able to hide/unhide shapes depending on selections a user makes on a userform. I've broken the problem down into a very simple example. If I insert a shape called "oval 1" in a sheet and run the code:
Sub hideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = False
End With
End With
End Sub
the shape disappears but when I run this code
Sub unhideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = True
End With
End With
End Sub
I get an error "Requested Shapes are locked for Selection"
The workbook is not protected and I have tried un-ticking locked and locked text on the shape properties.
Any ideas what's causing this.
You cannot Select a hidden object. However, you dont need to use Select at all, and it is usually not recommended. Try simply:
Sub HideShape()
ActiveSheet.Shapes("Oval 1").Visible = False
End Sub
Sub UnhideShape()
ActiveSheet.Shapes("Oval 1").Visible = True
End Sub
I hide shapes based on their name since some shapes I don't want to hide. I use this format:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
name of your shape or shapes goes into the array
if it only 1 shape you could just use:
Sheet1.Shapes.range("COtxtBox1").Visible = True
I found that the "mso" part is not necessary for the True or False statement
Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub
from: extendoffice.com
I solved problem with this code(Oval = Type 9, from MsoAutoShapeType Enumeration (Office)):
Sub hide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = False
Next i
End Sub
Sub unhide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = True
Next i
End Sub
If "Type = 9" is wrong, you can find out type of your shape with code in Immediate window (ctrl+G in VBA):
?ActiveSheet.Shapes("Oval 1").Type
Public HIDE As Boolean
Sub T_BUTTON ()
ActiveSheet.Shapes("T 1").Visible = HIDE
If ActiveSheet.Shapes("T 1").Visible = False Then
HIDE = True
Else
HIDE = False
End If
END SUB
Help!
Below is my code for a start/stop button in Excel and now I'm getting a Compile error that is highlighting btnStart on the first line.
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
Me.btnStart().Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
If I understand correctly the buttons are on an excel sheet (not in a user form) so your issue because you are calling the buttons incorrectly. you can't simply call the named buttons you must look into the sheet.buttons property like this
ActiveSheet.Buttons("btnStart").
Also if you have named sheets I would use the sheetname rather than ActiveSheet
Sheets("sheetname").Buttons("btnStart").
One more note, that enabling/disabling a button works BUT it doesn't make the button appear enabled/disabled. To do this you also have to change the font color.
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15 '15 is grey, 1 is black
---- edit: code changed ---
REASON: After doing some more research it seems there are problems with my original solution. The most important is that the "enabled" property has no effect in excel 2010. Another route would be using activeX controls, BUT, a recent windows update (dec 2014) prevents activeX controls from running without deleting some system files (which would have to be done for each user, on every computer this code may run on -_- good job MS SOURCE)
This new solution should avoid all those problems. It uses two global variables start_btn_disabled and stop_btn_disabled I assume each button in your form (btnStart and btnStop) have a macro assigned to them? Simply check the global variable at the very beginning of the the sub if the button is disabled then quit the sub, so even though the click is still processed (it will always be processed in excel 2010 as stated before) the code doesn't run. So it behaves as though it was disabled. In my code I made a sub called btnStopClicked that would run when you click 'btnStop' In order to assign the macro to a button, right click the button, select "assign macro" and select the appropriate macro. I also created a similar sub for when you click the start button
'these are global variables and should be declared at the top of the module
'outside of any sub/function
'
'Note we use DISabled rather than enabled, because by default
'booleans = False. This means as soon as the form opens both these buttons
'will be enabled without any extra work.
'If you want to change this (make start button enabled and stop disabled,
'when the workbook opens simply change all the "stop_btn_disabled" to
'"stop_btn_enabled" and uncomment the following line) make sure you change the
'variable names so they make sense
'Dim stop_btn_enabled As Boolean 'initializes to false
Dim start_btn_disabled As Boolean 'intializes to false
Dim stop_btn_disabled As Boolean 'intializes to false
'Most of this code remains the same as before
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'now we set the state of the global variable
start_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 15
'now we set the state of the global variable
stop_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 1
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
'now we set the state of the global variable
stop_btn_disabled = True
'makes the button appear greyed out
ActiveSheet.Buttons("btnStop").Font.ColorIndex = 15
'now we set the state of the global variable
start_btn_disabled = False
'makes the button black like normal
ActiveSheet.Buttons("btnStart").Font.ColorIndex = 1
End Sub
'and now the real key is checking the globals before running the code
'when you click "btnStop" button this is the code that runs, you may have
'named the sub something different, I just named it this way so it's clear
'what the sub does
Sub StopBtnClicked()
'must be the first bit of code in the btn click sub
If (stop_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click stop button goes here
'the only way to get to this point is if the but is enabled
End Sub
Sub StartBtnClicked()
'must be the first bit of code in the btn click sub
If (start_btn_disabled) Then
Exit Sub
End If
'the rest of the code when you click start
End Sub
if this solves your problem, please mark it as the answer
Maybe try the following, I corrected one line
Sub btnStart()
ActiveSheet.Unprotect
Cells(Rows.Count, 5).End(xlUp).Offset(1) = Date
Cells(Rows.Count, 6).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Cells(Rows.Count, 8).End(xlUp).Offset(1) = Environ("username")
'Corrected line below
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Sub btnStop()
ActiveSheet.Unprotect
Cells(Rows.Count, 7).End(xlUp).Offset(1) = Now
Cells(Rows.Count, 7).End(xlUp).NumberFormat = "hh:mm"
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub
Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You need to use ActiveX buttons if you want to use the Enabled property:
Private Sub btnStart_Click()
Me.btnStart.Enabled = False
Me.btnStop.Enabled = True
End Sub
Private Sub btnStop_Click()
Me.btnStart.Enabled = True
Me.btnStop.Enabled = False
End Sub