Formatting VBA Userform Text boxes Without Deleting the Actual Contents

Neileee

New Member
Joined
Oct 26, 2016
Messages
3
I have userform text boxes with dates in them in the format "dd/mm/yyyy". I want to be able to show however only the "dd". The issue is that if I use this code:

Code:
Label20 = Format(Label20, "dd")

Then the actual contents of my data gets lost. Ie using the code

Code:
msgbox label20

returns only the day value and not the entire date.

Is there a way to format the text box so only the day value shows but still keeps the entire data?

Thanks!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
One technique would be to have the .Text property of the text box hold (and show) the formatted value, while the .Tag property holds the actual value.

If you use code like this, you will have to look to the .Tag property for the date the text box holds, as in the CommandButton1_Click code below.
Code:
Private Sub TextBox1_Change()
    If TextBox1.Parent.ActiveControl.Name <> TextBox1.Name Then
        ProcessTextBoxDate TextBox1
    End If
End Sub

Private Sub TextBox1_AfterUpdate()
    ProcessTextBoxDate TextBox1
End Sub

Private Sub ProcessTextBoxDate(aTextbox As MSForms.TextBox)
    Dim dtTemp As Date

    With aTextbox

        If IsDate(.Text) Then
            .Tag = CDate(.Text)
        ElseIf 1 <= Val(.Text) And Val(.Text) <= 31 Then
            If Not IsDate(.Tag) Then .Tag = CDate(Int(Now()))
            dtTemp = DateValue(.Tag)
            .Tag = DateSerial(Year(dtTemp), Month(dtTemp), Val(.Text))
        Else
            .Tag = .Text
        End If

        TextBox1.Text = Format(.Tag, "dd")
      
    End With
End Sub

Private Sub CommandButton1_Click()
    MsgBox "The date in TextBox1 is " & CDate(TextBox1.Tag)
End Sub
 
Last edited:
Upvote 0
On further reflection, using the .ControlTipText property to store the underlying full date would be a better user interface. The user can see the dd value in the TextBox and see the full date by hovering over the text box.

Code:
Private Sub TextBox1_Change()
    If TextBox1.Parent.ActiveControl.Name <> TextBox1.Name Then
        ProcessDateTextBox TextBox1
    End If
End Sub

Private Sub TextBox1_AfterUpdate()
    ProcessDateTextBox TextBox1
End Sub

Private Sub ProcessDateTextBox(aTextbox As MSForms.TextBox)
    Dim dtTemp As Date
    With aTextbox

        If IsDate(.Text) Then
            .ControlTipText = CDate(.Text)

        ElseIf (1 <= Val(.Text)) And (Val(.Text) <= 31) Then
            If Not IsDate(.ControlTipText) Then .ControlTipText = Date
            dtTemp = DateValue(.ControlTipText)
            .ControlTipText = DateSerial(Year(dtTemp), Month(dtTemp), Val(.Text))

        Else
            .ControlTipText = 0
        End If
       
    End With
End Sub

Private Sub UserForm_Initialize()
    ProcessDateTextBox TextBox1
End Sub

Private Sub CommandButton1_Click()
    MsgBox "The date in TextBox1 is " & CDate(TextBox1.ControlTipText)
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top