Checkboxes in a range - excel

I have a sheet with checkboxes and names. I need one specific cell to show only the value of the cell related to the checkbox. For example.
when I press the checkbox for Lorena the cell A1 shows Lorena, if I check the one for Ricardo the cell A1 must show Ricardo. Is there any way to do it? I need to work with a range of 15 records.
Thank you!

An option button is going to work much better in this situation since it sounds like the there will only be one selected value, a collection of checkboxes won't work. The option button will ensure that only one value is selected at any time. I have an example set of code below for a set of three and attached images of it working. In order to achieve 15 records, it would simply be a matter of expanding from 3 to 15 option buttons.
In order to make this example work, simply add three option buttons and leave them with their default name. Then use the "Click" event that they have by default to trigger a function to post their caption into the cell A1. Make sure the option buttons have a caption of the name you want entered.
Option Explicit
Sub PrintName(name As String)
Cells(1, 1).Value = name
End Sub
Private Sub OptionButton1_Click()
PrintName OptionButton1.Caption
End Sub
Private Sub OptionButton2_Click()
PrintName OptionButton2.Caption
End Sub
Private Sub OptionButton3_Click()
PrintName OptionButton3.Caption
End Sub

Related

How do I use the FollowHyperlink worksheet event to recognize a hyperlink within a shape?

I've got two buttons on a worksheet that I've named "RemoveButton" and "AddButton". I've also added hyperlinks to both shapes and both shapes will point to the same cell once clicked. When I click both buttons, they point to cell A1 as expected, but the FollowHyperlink code does not recognize that a hyperlink has been clicked.
I wanted to use the FollowHyperlink worksheet event to recognize the shape that is clicked. I created the macro as below:
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Debug.Print "Clicked!"
End Sub
When clicking on the shapes, they just point to A1 and "Clicked!" never shows in my Immediate window. However, I created a test hyperlink that is text only and when selected, "Clicked!" appears. This indicates that Excel isn't treating the buttons as hyperlinks even though they have hyperlinks added to them.
The reason for the hyperlinks on the shape is for them to run code. I could use the assign macro feature to the shape, but in doing so I wouldn't be able to add a ScreenTip to the shape. I really want the ScreenTip as this will help future users know what the button is for.
Can someone please help me understand if this is possible?
Screenshot of buttons
There is a workaround for this problem. Instead of the Worksheet_FollowHyperlink event, you can use the Worksheet_SelectionChange event.
To do this, you need a cell that is completely covered up by your button. If the button is too small to cover up a cell, you can just hide a row and a column and place the button at the intersection of the hidden row and column.
Now, we link the button with the "hidden" cell, C5 in this example:
Now the hidden cell can only be selected by clicking the button.
So if the Target in the Worksheet_SelectionChange event is the cell C5, we know that the button has been clicked.
To leave the previous selection unaffected, you can use the following code in the worksheet's code module:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Const HIDDEN_CELL_ADDESS As String = "$C$5" '<--Set hidden cell address here
Static previousSelection As Range
If Target.Address = HIDDEN_CELL_ADDESS Then
'Make sure the linked cell doesn't stay selected, otherwise the next
'click on the button may not be recognized
Application.EnableEvents = False
If Not previousSelection Is Nothing Then previousSelection.Select
If TypeName(Selection) = "Range" Then
If Selection.Address = HIDDEN_CELL_ADDESS Then Target.Offset(1).Select
End If
Application.EnableEvents = True
Call ShapeClicked
Else
Set previousSelection = Target
End If
End Sub
Sub ShapeClicked()
MsgBox "The button has been clicked"
End Sub
GWD's answer correctly solves the issue. I also found a slightly different way to work around this as well. Please see below:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B11")) Is Nothing Then
Call dispatchLink(Target.Address)
End If
End Sub

Detect ComboBox Change Excel VBA

I have a sheet with a bunch of ComboBoxes(form control) and I want to detect when a user changes any one of them and write text in a cell. Using Worksheet_Change on the target cells doesn't work. I have tried a bunch of things that don't work. I'm not sure what needs to be in the private sub line or the if statement.
Private Sub DropDowns_DropButtonClick()
If ActiveSheet.DropDowns.Value > 1 Then
Cells(13, 5).Font.Bold = True
Cells(13, 5).Font.Color = vbRed
Cells(13, 5).Value = "!!! Selections have been changed. !!!"
End If
End Sub
I have tried
ComboBox_AfterUpdate()
ComboBox_Change()
DropDowns_AfterUpdate()
DropsDowns_Change()
and anything else I could find. I've also tried a few different things in the if statement with no luck.
I appreciate any help.
Chris
If I'm reading you correctly, you're comboboxes are in a userform. If I'm correct, simply open your userform in 'Visual Basic' and double click on the relavant combobox. This will open the code pane and create an empty Private Sub routine called 'Private Sub <Combobox Name> ()'.
Enter your code to place your data in the sheet (or whatever else you want) into the subroutine and Bob should be your uncle.
Apologies in advance if there's something I've missed.
RannochRob
Edit...
OK, my mistake, it's a form control.
My first comment is that it's easier to use an activex control if you can... however, with a form control, should (a) Use the cell link box in the 'Format Control' drop down ('Control' tab) to place the result in a cell... however, that result will not be the content of the box but an integer equal to the position of the selected entry on the list of entries in the combobox. You then need to (b) assign a macro to the combobox which will pick up the result and use it to get the required information from the range containing the list of entries. Like I say, much easier with an activex control...
RannochRob
Here's how you can do it by assigning a macro to the combobox (right click on the combobox>assign macro) as #BigBen mentioned in the comments section:
Option Explicit
Sub DropDown1_Change()
Dim sht As Worksheet
Dim dd As DropDown
Set sht = ThisWorkbook.Worksheets("Name of your Worksheet") 'Name of the worksheet in which the combobox is located
Set dd = sht.DropDowns("Drop Down 1") 'name of your combobox
sht.Range("G1").Value = "The selected value is: " & dd.List(dd.Value) 'dd.value returns the index of the selected value
End Sub
You can use the same code for each one of your comboboxes.
For demonstration purposes i have used the following set-up:
You can easily modify the code to best fit your needs.

Change active sheet based on selected item from a dropdown list

I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub

VBA: How to trigger event when selected value in combobox changes

I am new to VBA and would like to trigger a simple change event when the selection in a combobox (within a UserForm) changes.
I tried both Private Sub myCombo_AfterUpdate() and Private Sub myCombo_Change() but in both cases nothing happens when I make select a different value in the combobox.
Is this the wrong approach for comboboxes ?
My Sub starts as follows as I want to compare the currently selected item in the dropdown with the value in a table:
Private Sub myCombo_AfterUpdate()
If Me.myCombo.Value = Worksheets(8).Range("A4") Then
'do stuff
End Sub
Many thanks for any help with this, Tim.

Pass UserForm textbox value to cell

Community, I'm moderately new to excel. I have a textbox called Box_One. This has been set up on my userform. All I want to do is have a cell value constantly equal whatever value is in the textbox.
I tried the following, but not quite sure how to implement properly
Home.Range("A2").Value = Box_One.Value
how about using the Change event of your text box to run the code you want? Something like
Private Sub TextBox1_Change()
Range("BU1").value = TextBox1.value ' specify the destination sheet and cell here
End Sub
I tested this real quick and it worked.

Resources