Pass UserForm textbox value to cell - excel

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.

Related

Select named range via VBA and let user edit cell directly

I have a small issue, but I cannot find the code for it. For simplicity I made a simple example.
I made a validation userform with a modeless listbox (lsb_dataval_man). This listbox is populated with named ranges with only the names that have an error or are empty. Because there are many tabs, the user can navigate quickly to the sheets and cells which need attention and modify or populate them.
When you doubleclick a named range in the listbox, it will go to that reference and users need to enter data directly. See the simplified code for this double click below.
With the command Application.Goto ThisWorkbook.names(named_range).RefersToRange it will select the cell, but I am not able to type in the cell directly as like when you would do a select. A user needs to click the cell again or click in the formula bar and enter data. How can I change my code so after going to the cell of the named range, the user can type directly? Perhaps this is related to the modeless form which is still active?
The code:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
'select the named range cell
Application.Goto ThisWorkbook.names(named_range).RefersToRange
End sub
This should work:
Private Sub lsb_dataval_man_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim named_range As String
named_range = Me.lsb_dataval_man
AppActivate ThisWorkbook.Windows(1).Caption 'switches focus from userform to workbook
ThisWorkbook.Names(named_range).RefersToRange.Select 'selects cell
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.

ActiveX textbox displayes dates as number

I have ActiveX textbox and linked cell that is I21. I21 is set to be a date but in ActiveX textbox I see number. What is wrong here?
Here are the properties:
I found no way to copy the format from the linked cell to the control, nor to directly set a format for the control. Therefore the only way I could figure is to use the Worksheet Change event to fill the TextBox control with the value on the worksheet. Unfortunately, this is a one-way street.
Paste the code below to the code module of the worksheet on which you have your textbox. Leave the ActiveX control's LinkedCell property blank.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LinkedCell As Range
Set LinkedCell = Range("I21")
With Target
If .Address = LinkedCell.Address Then
Me.TextBox1.Value = Format(LinkedCell.Value, "dd mmm yyyy")
End If
End With
End Sub
Variatus suggestion works fine. However I have tried myself another approach and decided to use this one. Originally posted here
Insert this to standard module:
Public Function FMT$(ByVal Value, ByVal strFormat)
FMT = VBA.Format$(Value, strFormat)
End Function
Then convert date by using this formula for example in cell I22: =FMT(I21;"dd.mm.yyyy")
Finally link ActiveX textbox to I22. Your date should be displayed in the correct way.

Excel VBA textbox to populate combobox

I'm after some help that has had me stumped for a while. Excuse the long explanation.
I have a combobox that populates from a range when the userform initializes. When I type into the combobox the preemptive text appears as it is supposed to. I then have a Change event for a textbox which populates based on what gets typed into the combobox. That part all works fine (I got that code from another site).
I have two ways to enter the data into the combobox, one is by typing, and the other is when the text in another text box changes, it also populates the combobox. I do this by "combobox1 = textbox1.value". Now here is the part when I am stumped. When using the combobox1 = textbox1 method, it doesn't work properly (or more so, how I want it to work). It enters the text that is contained in textbox1, but it doesn't show the full line of preemptive text like how it does when typing in the combobox, nor does it then populate the textbox that changes when the combobox changes. If I then click in the combo box and hit the space bar the change event fires and the rest of the preemptive text from the range appears. I tried putting a space " " command at the end combobox1 = textbox1 & " " in the hope it would think there is more text to come but that didn't work. Is there any way to get VBA do do this, or am I asking too much of it?
Hopefully this makes sense.
cheers
paul
seems like ComboBox AutoCompletion feature is triggered by UI input only
you can work around it as follows:
Private Sub TextBox1_Change()
Dim iList As Long
With Me.ComboBox1
For iList = 0 To .ListCount - 1
If Left(.List(iList), Len(Me.TextBox1.Value)) = Me.TextBox1.Value Then
.ListIndex = iList ' if any combobox1 value matches textbox1 value then select it
Exit Sub
End If
Next
.ListIndex = -1 ' if no combobox1 value matches textbox1 value then "deselect" combobox1
End With
End Sub

Checkboxes in a range

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

Resources