Date in Userform to Worksheet..

johnohio

New Member
Joined
May 12, 2005
Messages
48
Office Version
  1. 2016
Platform
  1. Windows
Hello All,
I have an excel userform that the data and is entered saved on a spreadsheet. I want the date to show as MM/DD/YY.
I have tried several suggestions for older posting but nothing seems to work.

I want cell B5 to show the date as MM/DD/YY once it is on the spreadsheet.
I cannot seem to get is to do that. Thanks!

VBA Code:
Private Sub UserForm_Initialize()

Me.TextBox1 = Worksheets("a").Cells(6, 1) 'name
Me.TextBox2 = Worksheets("HOME").Cells(5, 7) 'begin bal
Me.TextBox3 = Worksheets("HOME").Cells(5, 2)  'begin date

Me.TextBox2 = Format(Me.TextBox2.Value, "$#,##0.00")
Me.TextBox3 = Format(Me.TextBox3.Value, "mm/dd/yy")

End Sub

Private Sub CommandButton1_Click()

If Me.TextBox1 = "" Or Me.TextBox2 = "" Or Me.TextBox3 = "" Then
    MsgBox ("Please complete all fields")
    Exit Sub
    End If
 
If IsNumeric(Me.TextBox2) = False Then
    MsgBox ("Opening Balance must be numeric")
    Exit Sub
    End If
    
If IsDate(Me.TextBox3) = False Then
    MsgBox ("Date is not valid")
    Exit Sub
    End If


x = MsgBox("Are you sure you want to save this change?", vbQuestion + vbYesNo, "STOP")
If x = vbNo Then
    Exit Sub
    End If
  
 
Worksheets("a").Cells(1, 4) = Me.TextBox1 'name at top
Worksheets("HOME").Range("b5").NumberFormat = "mm/dd/yy" '<<<<<<<< CAN'T GET TO WORK <<<<<<<<
Worksheets("HOME").Cells(5, 2) = Me.TextBox3


Worksheets("HOME").Cells(5, 7) = 1 * Me.TextBox2
Worksheets("HOME").Cells(5, 3) = "Opening Balance"
Worksheets("HOME").Cells(5, 4) = ". . . . ."


Worksheets("HOME").Cells(5, 11) = 1 * Me.TextBox2
Worksheets("HOME").Cells(5, 12) = 1 * Me.TextBox2
Worksheets("HOME").Cells(5, 7) = Me.TextBox2
    
Unload Me

End Sub
 

Attachments

  • Capture.GIF
    Capture.GIF
    11.4 KB · Views: 8

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Replace this lines:
VBA Code:
Worksheets("HOME").Range("b5").NumberFormat = "mm/dd/yy" '<<<<<<<< CAN'T GET TO WORK <<<<<<<<
Worksheets("HOME").Cells(5, 2) = Me.TextBox3

For this:
VBA Code:
Worksheets("HOME").Range("B5").Value = CDate(Me.TextBox3.Value)
Worksheets("HOME").Range("B5").NumberFormat = "mm/dd/yy"
In the textbox the data is stored as text, in this case, you must convert that text to date, with the CDate function.


--------------
Note: To optimize your code:
You can reference the sheet using the With statement
That way you only write the name of the sheet once. Notice how each line that will refer to the sheet is preceded by a dot.

VBA Code:
Private Sub CommandButton1_Click()
  If Me.TextBox1 = "" Or Me.TextBox2 = "" Or Me.TextBox3 = "" Then
    MsgBox ("Please complete all fields")
    Exit Sub
  End If
  If IsNumeric(Me.TextBox2) = False Then
    MsgBox ("Opening Balance must be numeric")
    Exit Sub
  End If
  If IsDate(Me.TextBox3) = False Then
    MsgBox ("Date is not valid")
    Exit Sub
  End If
 
  x = MsgBox("Are you sure you want to save this change?", vbQuestion + vbYesNo, "STOP")
  If x = vbNo Then
  Exit Sub
  End If
 
  Worksheets("a").Cells(1, 4) = Me.TextBox1 'name at top
 
  With Worksheets("HOME")
    .Range("B5").Value = CDate(Me.TextBox3.Value)
    .Range("B5").NumberFormat = "mm/dd/yy" '<<<<<<<< CAN'T GET TO WORK <<<<<<<<
 
    .Cells(5, 7) = 1 * Me.TextBox2
    .Cells(5, 3) = "Opening Balance"
    .Cells(5, 4) = ". . . . ."
 
    .Cells(5, 11) = 1 * Me.TextBox2
    .Cells(5, 12) = 1 * Me.TextBox2
    .Cells(5, 7) = Me.TextBox2
 
    Unload Me
  End With
End Sub

;)
 
Last edited:
Upvote 0
Solution
Replace this lines:
VBA Code:
Worksheets("HOME").Range("b5").NumberFormat = "mm/dd/yy" '<<<<<<<< CAN'T GET TO WORK <<<<<<<<
Worksheets("HOME").Cells(5, 2) = Me.TextBox3

For this:
VBA Code:
Worksheets("HOME").Range("B5").Value = CDate(Me.TextBox3.Value)
Worksheets("HOME").Range("B5").NumberFormat = "mm/dd/yy"
In the textbox the data is stored as text, in this case, you must convert that text to date, with the CDate function.


--------------
Note: To optimize your code:
You can reference the sheet using the With statement
That way you only write the name of the sheet once. Notice how each line that will refer to the sheet is preceded by a dot.

VBA Code:
Private Sub CommandButton1_Click()
  If Me.TextBox1 = "" Or Me.TextBox2 = "" Or Me.TextBox3 = "" Then
    MsgBox ("Please complete all fields")
    Exit Sub
  End If
  If IsNumeric(Me.TextBox2) = False Then
    MsgBox ("Opening Balance must be numeric")
    Exit Sub
  End If
  If IsDate(Me.TextBox3) = False Then
    MsgBox ("Date is not valid")
    Exit Sub
  End If
 
  x = MsgBox("Are you sure you want to save this change?", vbQuestion + vbYesNo, "STOP")
  If x = vbNo Then
  Exit Sub
  End If
 
  Worksheets("a").Cells(1, 4) = Me.TextBox1 'name at top
 
  With Worksheets("HOME")
    .Range("B5").Value = CDate(Me.TextBox3.Value)
    .Range("B5").NumberFormat = "mm/dd/yy" '<<<<<<<< CAN'T GET TO WORK <<<<<<<<
 
    .Cells(5, 7) = 1 * Me.TextBox2
    .Cells(5, 3) = "Opening Balance"
    .Cells(5, 4) = ". . . . ."
 
    .Cells(5, 11) = 1 * Me.TextBox2
    .Cells(5, 12) = 1 * Me.TextBox2
    .Cells(5, 7) = Me.TextBox2
 
    Unload Me
  End With
End Sub

;)
PERFECT!
And thanks for the coding tip!
 
Upvote 0

Forum statistics

Threads
1,215,094
Messages
6,123,069
Members
449,092
Latest member
ipruravindra

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