how do i format text output as date in vba?

ibruzzi

New Member
Joined
Jun 25, 2021
Messages
26
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I have 3 text boxes, accompanied by 3 spinbuttons. spinbuttons put day, month, year into the text boxes. When I enter the values into a cell, it's not in a date format. How do I convert it to a date in vba?

Here's the code I used.

VBA Code:
Private Sub SpinButton1_Change()

If Me.SpinButton1.Value = 0 Then Me.SpinButton1.Value = 31
If Me.SpinButton1.Value = 32 Then Me.SpinButton1.Value = 1

Me.day2.Value = Format(Me.SpinButton1.Value, "00")

End Sub

Private Sub SpinButton2_Change()
If Me.SpinButton2.Value = -1 Then Me.SpinButton2.Value = 12
If Me.SpinButton2.Value = 13 Then Me.SpinButton2.Value = 1

Me.month1.Value = Format(Me.SpinButton2.Value, "00")

End Sub

Private Sub SpinButton3_Change()
If Me.SpinButton3.Value = -1 Then Me.SpinButton3.Value = 2021
If Me.SpinButton3.Value = 2040 Then Me.SpinButton3.Value = 2021

Me.year1.Value = Format(Me.SpinButton3.Value, "00")
End Sub

Private Sub CommandButton2_Click()
Dim x As Long
Dim y As Worksheet
Set y = Sheet2
x = y.Range("A" & Rows.Count).End(xlUp).Row + 1
With y
.Cells(x, 1).Value = Me.day2.Value & ":" & Me.month1.Value & ":" & Me.year1.Value 

End With
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Try
VBA Code:
With y
.Cells(x, 1).Value = DateSerial(Me.year1.Value, Me.month1.Value, Me.day2.Value)

End With
 
Upvote 0
Solution

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

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