pass to textbox on a form and to excel worksheet

C

cvw

I have a calendar on a user form, along with 2 textboxes. The code works
thus far. The user must click in a textbox and click a date in the
calendar. I want the dates to also go to excel worksheet. Now it will if I
click on each textbox with the dates a second time. How can I get the dates
to go without that second click. My code is below: (NOTE: This is code I got
from the community site and revised for my needs)

Option Explicit
Dim LastTextBox As MSForms.TextBox
Private Sub TextBox1_Enter()
'Gives focus to textbox on userform
Set LastTextBox = Me.TextBox1
Worksheets("Part_Time_Payroll").Range("A2").Value =
Format(TextBox1.Text, "mm-dd-yyyy")

End Sub
Private Sub TextBox2_Enter()
'Gives focus to textbox on userform
Set LastTextBox = Me.TextBox2
Worksheets("Part_Time_Payroll").Range("B2").Value =
Format(TextBox2.Text, "mm-dd-yyyy")
End Sub

Private Sub Calendar1_Click()
'If a textbox is not selected, puts message on screen to select a textbox on
userform
If LastTextBox Is Nothing Then
MsgBox "Select a textbox first!"
Exit Sub
Else
'If a textbox is selected, puts date chosen in the selected textbox on
userform
LastTextBox.Value = Format(Calendar1.Value, "mmmm dd, yyyy")
End If
End Sub
Private Sub cmdClose_Click()
Unload Me
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top