Date inserted on userform wont work

elynoy

Board Regular
Joined
Oct 29, 2018
Messages
160
Office Version
  1. 365
  2. 2021
  3. 2016
Platform
  1. Windows
Hello once again.

I have a user form that gets data from one sheet and i want to edit that data and save it back to the same exact place.

the code I have works fine but while saving from the userform to the cell it wont work.

Here's the code I have to populate the userform:
Code:
Private Sub UserForm_Initialize()    Me.StartUpPosition = 0
    Me.Top = (Application.Height - Me.Height) / 2
    Me.Left = (Application.Width - Me.Width - 500)
    
    
'first tab


Me.tbA1i = sheets("ferias").Range("D7").Value
Me.tbA1f = sheets("ferias").Range("E7").Value


'second tab


Me.tbA2i = sheets("ferias").Range("D8").Value
Me.tbA2f = sheets("ferias").Range("E8").Value

End Sub

It's a 2 pages userform.

the code for the save button:
Code:
Private Sub CommandButton1_Click()    
    sheets("ferias").Range("D7").Value = Me.tbA1i
    sheets("ferias").Range("E7").Value = Me.tbA1f
    sheets("ferias").Range("D7").Value = Me.tbA2i
    sheets("ferias").Range("E7").Value = Me.tbA2f

    
End Sub

The problem is, what I'm typing into the userform are dates. 01-01-2019 for exemple. the userform inserts the right date exactly how I type inside the userform but then I have a condition formating and it wont assume the date entered in the respective cell unless I type manually inside that cell. Then the conditional formatting will work.

this is what I'm trying to achieve:

s6lqo0.png






10ynt45.png


If I type the dates 01-01-2019 and 15-01-2019 with the userform it wont turn green.

Best regards,
eLy
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
try these
Code:
Sheets("ferias").Range("D7").Value = CLng(Me.tbA1i)
or
Sheets("ferias").Range("D7").Value = CDate(Me.tbA1i)
 
Upvote 0
CDate works but if the cells are empty it gives type 13 mismatch error.

and if I open the userform again the dates it shows are diferrent from what I put in it.

I put 01-01-2019 and 15-01-2019. when I oepn the userf orm it shows like this:
r8tcmu.png


Best regards,
eLy
 
Last edited:
Upvote 0
Do you mean if the textboxes are empty?
If so, you'll need to check that they aren't empty before using that code.
 
Upvote 0
yes, some text boxes are empty, not all of them need to have dates in them.

how can I do what you suggest? what do you mean by check that they arent empty? Sopme of them are supposed to me empty.

best regards,
eLy
 
Upvote 0
You're image does little to help as I have no idea what it represents. ;)

Also why are you putting values into D7 & E7, only to immediately overwrite them, with other values?
 
Upvote 0
You're image does little to help as I have no idea what it represents. ;)

Also why are you putting values into D7 & E7, only to immediately overwrite them, with other values?

Sorry I thought the image would help to show that It's supposed to be dd/mm/yyyy and not mm/dd/yyyy.

and I'm not replacing them immmediatly. I dont know why you said that.

best regards,
eLy
 
Last edited:
Upvote 0
Oh I see, on my code, one of those D7 and E7 are supposed to be D8 and E8. sorry
 
Upvote 0
For the command button try
Code:
Private Sub CommandButton1_Click()
   On Error Resume Next
   Sheets("ferias").Range("D7").Value = CDate(Me.tbA1i)
   Sheets("ferias").Range("E7").Value = CDate(Me.tbA1f)
   Sheets("ferias").Range("D8").Value = CDate(Me.tbA2i)
   Sheets("ferias").Range("E8").Value = CDate(Me.tbA2f)
   On Error GoTo 0
End Sub
And at the end of the Userform Initialize add
Code:
Me.tbA1i = Format(Me.tbA1i, "dd/mm/yyyy")
do this for all the textboxes
 
Upvote 0
Do I need to make all textboxes now or I can use just 1 to test? Doing it to tast only on one gives me the same error.
and the userform initialize doesnt load anything
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

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