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
Related
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?
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", "")
I am trying to keep track of changes (to be appeared in a chart) upon every changes made in the worksheet using this reference (codes are copied below as example reference).
The changes in the worksheet actually can be made by several List Boxes (form Control Menu) and other cells e.g. Input 3 and 4. Let's say the resulting value caused by any change appears in the cell G14, and as the example code I want to keep them in I14 (as in the example image).
The formula might be e.g. D14 + E14 + K14 + L14 where K14 and L14 are values linked to the ListBoxes.
Selections in Input 3 and 4 trigger Worksheet_Change but selections in List Box apparently don't, shall I add a macro for List Box callback in which Worksheet_Change gets triggered?
Apart from that, if there is any better saving the results method, please mention it.
Many thanks in advance!
Dim xVal As String
Private Sub Worksheet_Change(ByVal Target As Range)
Static xCount As Integer
Application.EnableEvents = False
If Target.Address = Range("G14").Address Then
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
Else
If xVal <> Range("G14").Value Then
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
xVal = Range("G14").Value
End Sub
As you are asking about trigger event to run a macro on selection of listbox value (form control), I believe this could be considered as answer. I am sure someone with better knowledge will improve it.
Worksheet_Change event occurs when a cell or range of cells is changed manually (not by any auto process like calculation or selection of listbox value)
There can be different scenarios
First
There is no link between calculation of cells affected by the listbox change. Say, in First ListBox, you select 10, and cell A2 is updated to 10 and cell A3 is calculated value changed to 30. Then You select in second listbox 15 and cell B2 is changed to 15 and cell B3 is calculated independently if A2 and A3 to 90.In this case these are independent listboxes and their results. So, in this case, you can assign macro to each listbox which will run every time you change value in each listbox.
Second
Dependent calculations: Say the desired result for tracking is in D3 which will be calculated only on selection of four listbox values in A2, B2, C2 and D2. In this case you will not like to run macro for every list change but only after selecting values in all listboxes and calculation of D3. So in that case, instead of assigning macros to all the listboxes you could assign it only to last listbox.
Third
By now you must have understood there are two events taking place. One is selection of listbox value and second is calculation. In the second scenario, if you want to run macro for every change in calculation, say when you select A2 and when you select B2, etc. then you can simply use calculation event instead of assigning macro to every listbox. It will run everytime when a value is changed causing worksheet to calculate.
For assigning macro to listbox (form control) --- You can directly assign a macro to the list box.. First create a macro in VBA . Then Just right click on the list box and click assign macro. then select a macro to be assigned. .. The macro will be run when you click the listbox to change the value
Also, as you want to track the result calculated with macro, you need the sheet to be calculated first. Start the macro with Worksheet.Calculate method to be safe (in case formula results are not updated for some reason).
Tahnks to #Naresh, solved the problem in the following way, any improvement editis more than welcome since I know the codes might seem inefficient!
Dim xVal As String
Public Function customRecorder(Target As String)
Static xCount As Integer
Application.EnableEvents = False
Range("I14").Offset(xCount, 0).Value = xVal
xCount = xCount + 1
Application.EnableEvents = True
End Function
Private Sub Worksheet_Calculate()
xVal = Range("G14").Value
customRecorder (Range("G14"))
End Sub
Is there any way to block cell without block list?
Ex: I have in cell A1 the list:
Brazil
USA
Ireland
If I block the cell using "protect sheet"
I can't select the list
If I block on VBA using the code application.cutcopymode = false
It works, but if the person paste from a notepad, for example, it doesn't work
Is there any way to block it?
Private Sub Worksheet_SelectionChange(ByVal Target as Range)
If intersect(Target, range("A1")) is nothing then Exit Sub
Application.CutCopyPaste = False
End sub
I don't think you can do so (curious if I'm wrong).
What you could do however:
1) Developers > Insert > Combobox
2) Align Combobox with cell A1
3) Assign the appropriate list and a linked cell
4) On the Protection tab, select the Locked check box
5) On linked cell make sure you deselect the Locked check box
6) Protect your worksheet
Pasting values in cell A1 no longer possible, users deleting/moving/shaping your combobox is disabled AND you can still use the validation list :)
I want to fill a range of cells let's say A1:A5 with the value that is in a drop down list let's say in B1, by clicking once with the mouse in cells A1:A5. The way i want it to work is to first select the value that i want from the dropdown list and then click in in any of the cells from range A1:A5, and only the cell selected changes the value to the value from the dropdown list. And also when i change the value from the dropdown list the cells that were previously filled by clicking them do not change automatically to the new value from drop down. Once clicked they remain to that value until clicked to another selected value.
Add data validation to B1, under "Allow:" you pick "List", under "Source:" your values like: Value1,Value2,Value3... .. or of course a range..
Paste the following in sheet code(eg Sheet1(sheet1))
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A5")) Is Nothing Then
Selection.Value = Range("B1").Value
Range("B1").Value = ""
End If
End Sub