Macro to Display String on Checkbox Select - excel

I have a checkbox on sheet1 (there are about 7 sheets total). If the checkbox is selected (true), I want it to say "Approved" in cell M9. If the checkbox is not selected (false), I want it to say "Denied" in the textbox.
Do I need to create a macro for that?
If I want it to display the same text in cell M5 of sheet2, how would I put it all together?

You can link the status of a checkbox to a cell (right-click, format control, cell link on the control tab), this means you can then refer to that cell in any other sheet or cell.

Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Range("M9").Value = "Approved"
Else
Range("M9").Value = "Denied"
Sheets("Sheet2").Range("M5").Value = "Denied"
End If
End Sub

Related

Excel VBA UserForm, input data from textbox in a cell, cell is based on value in combobox

I am having an excel file with a bunch of UserForms, they are all working well. but I got stuck on one part.
I have a userform with a combobox1 and a textbox1.
in the combobox you can select 17,19,21,23,25,25+
in the textbox a numeric value should be typed, for e.g. 80
based on the selected value in the Combobox1 (17,19,21,23,25,25+) I want the data from the textbox (80) to be inserted in respectively column H,I,J,K,L or M of the active row. yet I cannot figure out how...
That's rather easy:
The items in the Combobox are indexed, the first item has index 0, the second 1 and so on. You can get the selected item with the property ListIndex. If nothing is selected, it returns -1, else the index of the selected item.
Now all you need is to use this as offset to the cell in the current row and column "H". Put the following sub into the form code and call it from whatever event routine you want (button, change-event of the textbox, form close...) Just change the names TextBox1 and ComboBox1 to the names of your controls.
Sub PutValueToSheet()
If Me.TextBox1 = "" Or Me.ComboBox1.ListIndex < 0 Then Exit Sub
Dim cell As Range
Set cell = ActiveSheet.Cells(ActiveCell.Row, "H").Offset(0, Me.ComboBox1.ListIndex)
cell.Value = Me.TextBox1
End Sub

Hide Columns with Dropdown Lists

I am creating a form which has a dropdown for the user to select which platform their conference will be on. So the dropdown has two values, "Type1" and "Type2". Depending on which they choose, I would like the user to see the options for that specific value, from which they can choose to add. These options cells have a "true/false" checkbox. So if Type 1 is selected, I only want to see the rows where "Type 1" options are. And if Type 2 is selected, I only want to see "Type 2" options. I was able to get this to work with a formula for the sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim TrigerCell As Range
Set Triggercell = Range("C29")
If Not Application.Intersect(Triggercell, Target) Is Nothing Then
If Triggercell.Value = "Type1" Then
Rows("38:42").EntireRow.Hidden = True
Rows("32:37").EntireRow.Hidden = False
ElseIf Triggercell.Value = "Type2" Then
Rows("32:37").EntireRow.Hidden = True
Rows("38:42").EntireRow.Hidden = False
End If
End If
End Sub
The problem I have is even though those rows are hidden, all of the check boxes are stacking into one cell. So the cells are hiding, but the checkboxes in those cells are not. Is there some way to make the checkboxes "stick" in the cell so when the cell is hidden, so is the dropdown. I also noticed that if I copy a cell with a dropdown above it and paste it into a new cell, it copies and pastes the checkbox for that cell and the one above it. What am I missing?

Excel VBA: Control multiple pass/fail checkboxes with one macro

I have a test document that will have a lot of pass/fail checkboxes on it. The checkboxes are ActiveX and when clicked, I need them to print "Pass" or "Fail" to a cell that they are located in within the Sheet. I need them printed to the sheet because Excel's track changes doesn't record when the checkbox is clicked. These checkboxes are not part of a userform.
In my example below, Checkbox7 and Checkbox8 are located in cell C14. I have over 50 groups of the pass/fail checkboxes and I am trying to figure out a way that all of the checkboxes be handled by 1-2 Subs instead of having one per each checkbox.
Private Sub CheckBox7_Click()
If CheckBox7 = True Then
Range("C14").Value = "Pass"
Else
Range("C14").Value = ""
End If
End Sub
Private Sub CheckBox8_Click()
If CheckBox8 = True Then
Range("C14").Value = "Fail"
Else
Range("C14").Value = ""
End If
End Sub
I don't think I can use the same approach found in this solution since I'm not using a userform. Any suggestions/help would be much appreciated
You can do this without code.
In each Checkbox's properties, set the LinkedCell property to the cell that is covered by the checkbox. Just enter the address, like F2. This cell will now have the value of TRUE or FALSE depending upon the status of the checkbox.
In the cell where you want the comment, enter this formula (referring to the cell you linked to the checkbox), =IF(F2 = TRUE, "Failed", "")

How can I hide a row in another Excel sheet with a ToggleButton?

I have a sheet that I want to put an active X control, the result I expect is to hide a row in another sheet when I click the button and then unhide if I click the button again.
Here is the code I've tried.
Private Sub PLK08_Click()
Dim MyC As String
MyC = Worksheets("stepcleaning").Rows("A12")
If PLK08.Value Then
Application.ActiveSheet.Rows(MyC).Hidden = True
Else
Application.ActiveSheet.Rows(MyC).Hidden = False
End If
End Sub
the result I expect is to hide a row in another sheet when I click the button and then unhide if I click the button again
This should be all you need.
Private Sub PLK08_Click()
ThisWorkbook.Worksheets("stepcleaning").Rows(12).Hidden = PLK08.Value
End Sub
Note that A12 is a cell while the row number is 12 and the column is A. Therefore a Rows("A12") does not exist but a Rows(12) or Columns("A") or Range("A12"). So if you want to hide a row it must be Rows(12).

Linking Excel ComboBox with List Based on Independent Cell

I'm attempting to add items to an Excel Combobox, based on the value of an independent cell. So what I need is an IF statement, obviously.
So it should look something like this, (note: C1 is the independent cell):
IF C1 = "3"
AddItem "One"
AddItem "Two"
ELSE IF C1 = "4"
AddItem "Three"
.etc.
The problem is that I don't know how to properly link the ComboBox such that it knows when the independent cell value has changed to trigger a clearing of existing items in the ComboBox and repopulation of new items.
This code needs to go on the sheet with your target cell C1
Every time your cell C1 is changed, it will force the code you insert to execute. In this case, it will force an update to your dropdown list.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("C1"), Range(Target.Address)) Is Nothing Then
Application.EnableEvents = False
'YOUR CODE HERE
Application.EnableEvents = True
End If
End Sub
Edit 1: (How to add items to a combo box?)
Add an ActiveX Control Combo Box
Developer Tab > Insert > ActiveX Controls > ComboBox
You can then refer to your Combo box inside your loop as follows:
Combobox1.Clear 'To clear
ComboBox1.AddItem "Text" 'To Add

Resources