Date function and update back issue - excel

I have a two part issue regarding the same item. I have built a contracts management style system that relies on a userform to populate the worksheet and then another userform to recall the data from that sheet. This bit works perfectly. However, there is a 'start date' and 'end date' part that transfers from the userform to the worksheet.
Problem 1:
When the date is entered in dd/mm/yyyy and the 'dd' part is 1-12, it translates to US format for the date. For 'dd' values 13-31, it works fine. I have used format date code and this makes no difference.
Problem 2:
On the userform, I have an 'update' command button that overwrites any changed data back to the correct row on the excel sheet. If the date gets updated (i.e. 'end date' extended) this just returns 'FALSE' back to the cell. When I reload the contract back into the userform, it shows 31/12/1899.
Please can anyone help on any of the above?
This is the snip of the code for writing the dates for a new contract
ws.Cells(Lastrow + 1, 18).Value = TextBox18.Value
ws.Cells(Lastrow + 1, 21).Value = TextBox19.Value
This is the snip of the code for updating from the userform back to the excel sheet
Cells(rowselect, 18) = Me.TextBox18.Value = Format(TextBox18.Text, "mm/dd/yyyy")
Cells(rowselect, 21) = Me.TextBox19.Value = Format(TextBox19.Text, "mm/dd/yyyy")
I have searched many posts on here and none of the things I have tried are any better.
Thank you in advance.

For Problem 1:
If you know that the TextBox always has a date in UK-style dd/mm/yyyy, then YOU should take control of how the cell gets setup rather than relying on Excel to decide. Try code like:
arr = Split(TextBox18.Value, "/")
Cells(1, 1).Value = DateSerial(arr(2), arr(1), arr(0))
Cells(1, 1).NumberFormat = "dd mmmm yyyy"
and assuming that the TextBox gives 10/1/2021, then the result would be:
For Problem 2:
In some languages:
alpha = beta = gamma
would "daisy-chain" like:
beta = gamma
alpha = beta
VBA does not work that way. VBA sees:
alpha = (beta = gamma)
and evaluates (beta = gamma) as a Boolean; True if beta equals gamma, otherwise False

thanks for your help. I managed to fix this problem with your help to the following,
Private Sub TextBox18_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
arr = Split(TextBox18.Value, "/")
Dim dtDate As Date
dtDate = DateSerial(arr(2), arr(1), arr(0))
Me.TextBox18.Value = Format(dtDate, "dd mmmm yyyy")
End Sub
This cures the issue with the date not swapping to US as the date auto-updates in the textbox to Long Date Format.
The update issue I had to use three seperate input boxes for day, month and year and have VB put them together before updating the cell.
Thanks once again for your help.

Related

Is there a way to convert a Long Date to short date in Excel?

I have an exported file that gives me a list of long dates in format General.
ex: Friday, August 28, 2020
I am trying to convert them into short dates. I've tried using CDate function, but I get a mismatch error. I find this odd because the cell has the exact long date form.
I've tried running a ton of code. Here's the most recent one I tried. It changs the cell format into Long Date. then uses Cdate and gets a mismatch error.
Sub formatdate()
'
' Macro1 Macro
'
Range("J2").Select
Selection.NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
MsgBox (CDate(Range("J2")))
End Sub
Any help would be much appreciated!
As #scottcraner says:
Dim v
With Range("J2")
v = .Value
v = Mid(v, InStr(v, ",") + 1, 100)
.Value = DateValue(v)
End With

application.doubleclick

i need to double click on a cell.and looks like its getting edited.this is needed because i am pasting a date to cell .but it is not getting formatted as date .so if i double click it its getting formatted as date .(this is a manual work and i need it automated.
i tried using application . doubleclick method.
Cells(i, "AW").Select
Application.doubleclick
Cells(i, "AX").Select
for example,if the date i "26-08-19 23:45",when i double click it it becomes "26-08-2019 23:45:00"
Here is a suggestion. Use a validation function that is called to check the value entered into Textbox6 is a valid date and if so, update the cell with the date. Below is sample code which you may need to adapt. If you are collecting lots of dates in many text boxes then you would need to have multiple _Change functions, one for each box, or alternatively do the validation all in one go after a button click or similar user action:
Private Function CheckDate(tb As MSForms.TextBox) As Variant
CheckDate = ""
Dim dd As Date
On Error Resume Next
dd = CDate(tb.Text)
If Err.Number <> 0 Then
tb.BackColor = vbYellow
ElseIf Year(dd) < 1900 Then
tb.BackColor = vbYellow
Else
tb.BackColor = vbWhite
CheckDate = dd
End If
End Function
Private Sub TextBox6_Change()
Me.Range("D9").Value = CheckDate(TextBox6)
End Sub
Empty box:
Valid date entered:
Many formats of date are supported
If you are pasting from a textbox try and force format it as the correct date right away:
Format(Sheet1.TextBox6.Value, "dd-mm-yyyy hh:mm:ss")

Excel Date/Time Lookup Error

I have two columns of 15 minute date/time interval data. One is formatted in 24-hour time and the other 12-hour. These come from two different sources, and reformatting the source data is not an option. All of these intervals are showing as exact matches (even as values), but when I use a lookup formula (INDEX/MATCH & VLOOKUP) or even VBA, a match for every third interval is not being identified. I have attached sample pictures with formulas as well as my code below. Thanks in advance!
Sub MatchTest()
Dim i As Integer
For i = 2 To 22
If CDate(Cells(i, 1).Value) = CDate(Cells(i, 3).Value) Then
Cells(i, 8) = Cells(i, 2)
ActiveCell.Offset(1, 0).Select
Else:
Cells(i, 8) = "NA"
ActiveCell.Offset(1, 0).Select
End If
Next i
End Sub
Instead of Cdate, Timevalue and Datevalue can be used can be used. The TIMEVALUE function returns a serial number of a time. The DATEVALUE function converts a date that is stored as text to a serial number that Excel recognizes as a date.
For instance, Timevalue of 08/12/2008 20:00:00 and 08/12/2008 08:00PM yields the same result 0.833333333335759
Datevalue of same date in different format will also yield same result
TIMEVALUE(TEXT(yourdate, "DD/MM/YYYY HH:MM:SS"))
TIMEVALUE(TEXT(yourdate, "DD/MM/YYYY HH:MM AM/PM"))
DATEVALUE(TEXT(yourdate, "DD MMMM YYYY"))
Hey all thanks for your answers. All three of these methods worked.
If TimeValue(Cells(i, 1).Value) = TimeValue(Cells(i, 3).Value) Then
If DateValue(Cells(i, 1).Value) = DateValue(Cells(i, 3).Value) Then
If Abs(CDate(Cells(i, 1).Value) - CDate(Cells(i, 3).Value)) < 0.000001 Then

Compare date times chosen with date and time picker in Excel VBA

So i have a multi-page form that uses two "Date and time Picker" controls named StartDate and EndDate. I want to ensure that the user does not enter the StartDate later than the EndDate. I have the following questions. Is the StartDate.value initially "" or is it null? Is what's returned by StartDate a string or a date? Here is what I have so far.
As a side remark I am also somewhat confused by this line of code even after reading the documentation.
emptyRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Private Sub StartDate_Change()
Dim emptyRow As Long
'Submits the date in the first empty row immediately since the form does not retain datepicker data after the page changes.
If (EndDate.Value) <> "" And CDate(StartDate.Value) >= CDate(EndDate.Value) Then
MsgBox ("Please enter a valid date")
MultiPage1.Value = 4
Else
Sheet1.Activate
emptyRow = Application.WorksheetFunction.CountA(Range("A:A")) + 1
Cells(emptyRow, 18).Value = StartDate.Value
End If
End Sub
DateTimePickers return Dates.
If you want to test that the EndDate is greater than StartDate, then code such as the following should do the trick:
(StartDate.Value < EndDate.Value)

VBA conversion failed when converting date and/or time from character string

An often asked question, when I search the internet, but none of the answers seem to fit my problem. I hope you can help.
I am trying to collect dates from a DB, using code:
Sheets("Data").Select
Dim sh1 As Worksheet
Set sh1 = ActiveWorkbook.ActiveSheet
sh1.Activate
If ActiveSheet.FilterMode Then
Range("ActivityPlan[[#Headers],[AIDA]]").Select
ActiveSheet.ShowAllData
End If
Dim lLastRow As Long
'Get Last row
lLastRow = sh1.Range("C" & Rows.Count).End(xlUp).Row
And having defined my values using the type of the three following examples
If Not sh1.Cells(i, 19) = "" Then
tMaintSimulStartDate = Format(sh1.Cells(i, 19), "MM/dd/yyyy")
End If
tMaintExpDate = Format(sh1.Cells(i, 20), "MM/dd/yyyy")
If Not sh1.Cells(i, 21) = "" Then
tUpBaseDteFix = Format(sh1.Cells(i, 21), "MM/dd/yyyy")
Else
tUpBaseDteFix = vbNull
End If
None of them seem to work. I just get the error "conversion failed when converting date and/or time from character string", VBA marking my " CN.Execute (SQL) "
I think it's very likely that the values in your cells sh1.cells(i,19), (i, 20), (i,21) are just strings. They may look like dates, but Excel doesn't recognize them as such.
Your best bet is to use DateValue() function in VBA. This will take in a string and spit out an actual, honest to goodness date. If this fails, it's because your Date format in the cell is something that DateValue() can't recognize. In which case you'll probably have to parse out the values of the date from the cell and then send it over to DateValue() to turned into a proper date.

Resources