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:
Is everything now working for those few textboxes?
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
yes, the they all insert the date correctly and loads correctly on all of them
 
Upvote 0
In that case all you need to do is expand the array
Code:
 CtrlAry = Array(tbA1i, "D7", tbA1f, "E7", tbB1i, "D9", tbB1f, "E9", tbC1i, "D11", tbC1f, "E11")
to include the other textboxes & the relevant range, in the same way.
 
Upvote 0
already did that, and the same for the second page of the userform, how do I implement the second page on the array?
and by the way, how to save it as a blank cell? since if i delete a date from the userform and press save it wont save as empty, only saves the date or another date.
 
Last edited:
Upvote 0
This will clear the cell if the textbox is blank
Code:
      For i = 0 To UBound(CtrlAry) Step 2
         If CtrlAry(i).Value = "" Then
            .Range(CtrlAry(i + 1)).ClearContents
         Else
            .Range(CtrlAry(i + 1)).Value = CDate(CtrlAry(i))
         End If
      Next i
I'm not sure what you mean by the second page on the array.
You just need one array for all textboxes,ranges
 
Upvote 0
the userform has 2 pages. that array, I just need to add the rest of the textboxes for the secon d page of the userform?
 
Upvote 0
With everything in the one array it works for me with a multipage userform
 
Upvote 0
your last code for clearing the contents if the cell is empty gives me an error on .Range. It says invalid or unqualified reference.

Edit: Nevermind, I was adding it to the wrong place. now I just need to add the rest of the textboxes to the array, it's too big it goes down a line and wont work. how can I "divide" so it goes down one line on the vba with "& _ "?
 
Last edited:
Upvote 0
Another thing that I noticed now is that it takes too long to save. like it the excel is about to crash, everything freezes
 
Upvote 0
To split the array over multiple lines do it like
Code:
   CtrlAry = Array(tbA1i, "D7", tbA1f, "E7", tbB1i, "D9", _
      tbB1f, "E9", tbC1i, "D11", tbC1f, "E11", tb1, "D8", tb2, "E8")
To speed up the code try
Code:
Private Sub CommandButton1_Click()
   Dim i As Long
   
   With Application
      .ScreenUpdating = False
      .Calculation = xlCalculationManual
      .EnableEvents = False
   End With
'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)).ClearContents
         Else
            .Range(CtrlAry(i + 1)).Value = CDate(CtrlAry(i))
         End If
      Next i
   End With
   With Application
      .ScreenUpdating = True
      .Calculation = xlCalculationAutomatic
      .EnableEvents = True
   End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,049
Messages
6,122,864
Members
449,097
Latest member
dbomb1414

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