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", "")
Related
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
My excel tool allow me to calculate prices.
There is this dropdown list cell that indicates the product, and another dropdown list cell to indicates the city where is should be delivered, but what I would like to do is to freeze country cell(set it to one value) if a certain value is selected for the product.
Working on Excel 2019
You're looking for Worksheet.Protect and Range.Locked.
Once the user has entered a value for product and you want to lock the country cells, you will need to do something like the following:
Activesheet.Unprotect
Range("Everything Except Country").Locked = False
Range("Country").Locked = True
Activesheet.Protect
Once you protect the sheet, the default for all cells is Locked, so you only need to define the areas where it should still be editable. If you want to continue locking additional cells as the user enters more data, you will need to unprotect the sheet, redefine the locked ranges and then re-protect the sheet.
I would suggest using the Worksheet_Change event with something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
if Target.Address = Range("Product").Address then
Activesheet.Unprotect
Range("Everything Except Country").Locked = False
Range("Country").Locked = True
Activesheet.Protect
end if
End Sub
You may also want to add an event or button where the sheet can reset to all cells being unprotected.
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
I am using a userform with multiple checkboxes in it. My goal is to have all checkboxes return their values (unique two letter acronyms) to a single cell. I am not sure how to accomplish this. I have no VBA experience prior to today.
You can record a macro to get your other relevant code for VBA but this should help:
Range("A" & LastRow).Value = CheckBox1.Value & CheckBox2.Value & CheckBox3.Value 'etc.
'Concatenate CheckBox values with "&" (the values will be True or False though)
Set linkcell of each checkbox to a different cell -.e.g. checkbox1 to cell A1, checkbox2 to cell A2, etc
For each link cell, use If formula in its adjacent cell to test the link cell's content and return a string or a blank,
e.g. if(A1=True,"AA","") in cell B1, if(A2=True,"BB","") in cell B2, etc
Finally, concatenate results of 'if' Formulas in another cell: e.g. B1&","&B2&","&B3, using comma as separaters
To do this, you can make a collection of all of the Checkboxes in the form. Heres my code:
Private mcolCHK As Collection 'This creates a modular level collection, which will keep track of all of the checkboxes
Private Sub cmdOK_Click()
Dim objA As Object, strResults As String, i As Integer
i = 1
For Each objA In mcolCHK
strResults = strResults & i & Left(objA.Value, 1)
i = 1 + i
Next
Unload frmTestForm 'rename this the name of your form
MsgBox strResults
'instead of using a message box, you can say "strResults = Sheets(sheet1).range("A1").value
'This part may be customized as needed
End Sub
Private Sub UserForm_Initialize()
Set mcolCHK = New Collection
mcolCHK.Add chkAtt1 'you will have to add one of these for each checkbox (simply replace "chkAtt1" with the name of your first checkbox
mcolCHK.Add chkAtt2 'and repeat for each checkbox
End Sub
Checkboxes always give a value of True or False. with this method, you will get the number of the checkbox (in order of how you added them to the collection in the UserForm_Initialize() sub) and the first letter of the result (T or F). When i ran this, I had two checkboxes, and checked each one when the form ran. This gave me a result of "1T2T". This can be changed as needed. All you really need is how this all works. just make sure to change all the names to match the names of the variables you used.
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