tjdickinson
Board Regular
- Joined
- Jun 26, 2021
- Messages
- 61
- Office Version
- 365
- Platform
- Windows
Hi, all,
I have a userform with 5 input fields, four of which are textboxes (the other is a combobox). Two of the fields ask the user to input a date in the format dd/mm/yy, and one of them asks for a number.
Currently, these values are added to the worksheet as text values, even though the cells are formatted as dates/numbers. (Thus it also flags that the input data doesn't correspond to the cell formatting).
I would like the dates to retain the day, month, and year data, but to only show dd/mm in the cell.
I would like the number to display as a number (allowing for decimals and zero).
Here's the code that adds the form data to the worksheet:
P.S. If you have any suggestions to optimise the code, I'm all ears!
P.P.S. Do I understand the code correctly that, if I have the worksheet protected, I can uncomment the lines
and
and replace "password" with the worksheet password?
I have a userform with 5 input fields, four of which are textboxes (the other is a combobox). Two of the fields ask the user to input a date in the format dd/mm/yy, and one of them asks for a number.
Currently, these values are added to the worksheet as text values, even though the cells are formatted as dates/numbers. (Thus it also flags that the input data doesn't correspond to the cell formatting).
I would like the dates to retain the day, month, and year data, but to only show dd/mm in the cell.
I would like the number to display as a number (allowing for decimals and zero).
Here's the code that adds the form data to the worksheet:
VBA Code:
Private Sub Add_Eval_Add_Click()
Dim iCol As Long
Dim ws As Worksheet
Set ws = Worksheets("Gradebook")
'find first empty column in database
iCol = ws.Cells.Find(What:="*", SearchOrder:=xlColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column + 1
'check for an evaluation title
If Trim(Me.Eval_Title.Value) = "" Then
Me.Eval_Title.SetFocus
MsgBox "Please enter an evaluation title."
Exit Sub
End If
'check for a category
If Trim(Me.Eval_Cat.Value) = "" Then
Me.Eval_Cat.SetFocus
MsgBox "Please choose a category."
Exit Sub
End If
'check for an assigned date
If Trim(Me.Eval_Date.Value) = "" Then
Me.Eval_Date.SetFocus
MsgBox "Please enter the date the evaluation was assigned."
Exit Sub
End If
'check for a due date
If Trim(Me.Eval_Due_Date.Value) = "" Then
Me.Eval_Due_Date.SetFocus
MsgBox "Please enter the due date."
Exit Sub
End If
'check for a points value
If Trim(Me.Eval_Points.Value) = "" Then
Me.Eval_Points.SetFocus
MsgBox "Please enter the available points."
Exit Sub
End If
'copy the data to the database
'use protect and unprotect lines,
' with your password
' if worksheet is protected
With ws
' .Unprotect Password:="password"
.Cells(1, iCol).Value = Me.Eval_Title.Value
.Cells(2, iCol).Value = Me.Eval_Cat.Value
.Cells(3, iCol).Value = Me.Eval_Date.Value
.Cells(4, iCol).Value = Me.Eval_Due_Date.Value
.Cells(5, iCol).Value = Me.Eval_Points.Value
' .Protect Password:="password"
End With
'clear the data
Me.Eval_Title.Value = ""
Me.Eval_Cat.Value = ""
Me.Eval_Date.Value = ""
Me.Eval_Due_Date.Value = ""
Me.Eval_Points.Value = ""
Me.Eval_Title.SetFocus
End Sub
P.S. If you have any suggestions to optimise the code, I'm all ears!
P.P.S. Do I understand the code correctly that, if I have the worksheet protected, I can uncomment the lines
VBA Code:
.Unprotect Password:="password"
VBA Code:
.Protect Password:="password"