ActiveX textbox displayes dates as number - excel

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.

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.

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

Excel VBA Combobox list in Module have selection then output to cell

Cant seem to find the answer to this, I need a combobox (which is inside a userform) that has three selection
eg Rental,Purchase and Finance
Which the selected input is then put into example cell A1. I know how to do this if I have the list already on my excel sheet, but I don't want the list on the excel sheet only the selection. so I suppose I'm asking for a coded list of option inside the module which then outputs the result in to a cell.
Any help would be appreciated (I'm a Newbie)
Thanks
Basic code in the userform:
Private Sub ComboBox1_Change()
If me.combobox1.listindex > -1 then sheets("Sheet name").Range("A1").value = me.combobox1.Value
End Sub
Private Sub UserForm_Initialize()
Me.ComboBox1.List = Array("Rental", "Purchase", "Finance")
End Sub

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