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:
Ok, try this
Code:
Private Sub CommandButton4_Click()
   Dim Ary As Variant
   Dim i As Long
   
   Ary = Array(tbA1i, "D7", tbA1f, "E7", tbB1i, "D9", tbB1f, "E9", tbC1i, "D11", tbC1f, "E11")
'parte 1 botão primeira página
   With Sheets("ferias")
      For i = 0 To UBound(Ary) Step 2
         If Ary(i).Value <> "" Then
            .Range(Ary(i + 1)).Value = CDate(Ary(i))
         End If
      Next i
   End With
End Sub
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
with that the error wont show up but i got another issue.if i delete the dates in the userform and prss save it wont save , i mean wont clear the cell. and the dd-mm-yyyy wont work either it insists on loading mm-dd-yyyy
 
Upvote 0
Let's just concentrate on one problem at a time.
Does the above code do you what you need?
 
Upvote 0
if you're asking if it inserts the dates where it should, yes. it works. but not how it's supposed to work.
 
Upvote 0
How is it supposed to work & in what way isn't it?
 
Upvote 0
I'll try to writhe the steps on how I tested it:

press button with userform.show
insert dates on the tbA1i and tbA1f
press save button and it saves correctly, the color change according to the conditional rule.
close the userform and load it again.
The dates returns to the mm-dd-yyyy instead of dd-mm-yyyy
insert a new date on tbB1i and tbB1f
it messes up because of the date change to mm-dd-yyyy
close the form and load it again the dates show correctly on the userform as dd-mm-yyyy
close and open agian, it messes up the dd-mm-yyyy agian and so on

it needs to be like this:
tbA1i start date and tbA1f end date
displayed always as dd-mm-yyyy on cell and userform
 
Last edited:
Upvote 0
The date format is completely irrelevant at the moment.
Does the code in post#21 put the dates into the correct cells without any errors?
 
Upvote 0
Ok Try this
Code:
[COLOR=#0000ff]Option Explicit
Dim CtrlAry As Variant[/COLOR]

Private Sub CommandButton4_Click()
   Dim i As Long
   
'parte 1 botão primeira página
   With Sheets("ferias")
      For i = 0 To UBound(CtrlAry) Step 2
         If CtrlAry(i).Value <> "" Then
            .Range(CtrlAry(i + 1)).Value = CDate(CtrlAry(i))
         End If
      Next i
   End With
End Sub
Private Sub UserForm_Initialize()
   Dim i As Long
    
   CtrlAry = Array(tbA1i, "D7", tbA1f, "E7", tbB1i, "D9", tbB1f, "E9", tbC1i, "D11", tbC1f, "E11")

   Me.StartUpPosition = 0
   Me.Top = (Application.Height - Me.Height) / 2
   Me.Left = (Application.Width - Me.Width - 500)
    
    
'PÁGINA 1 INICIO E FIM
   
   For i = 0 To UBound(CtrlAry) Step 2
      With Sheets("ferias")
         CtrlAry(i).Value = Format(.Range(CtrlAry(i + 1)).Value, "dd/mm/yyyy")
      End With
   Next i
End Sub
The two lies in blue must go at the very top of the module, before any code.You will need to turn your existing code into comments
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,217
Members
448,951
Latest member
jennlynn

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