Finding difference and Comparing dates in VBA - vba

I am trying to write a code that takes in a date as input and checks whether or not it is later than the date defined in the code.
Sub times()
Dim d1 As Date
n = Application.InputBox("Enter a date in mm/dd/yyyy format: ")
entered_date = CDate(entered_date)
d1 = 5 / 11 / 2021
If d1 > entered_date Then
d2 = DateDiff("D", d1, entered_date)
MsgBox ("late by " & d2)
Else
MsgBox ("on time")
End If
End Sub
the date diff function doesn't seem to be working for me or something is wrong my logic.
thanks in advance!

Well here is my answer. And as #NicholasHunter says, Option Explicit is always better!
It is clear that you figure it out, but consider this answer, anyway hope to help someone else.
Option Explicit
Sub times()
'Here you can validate the format date type for your system...
Dim SystemDateType As Integer
Dim SysFormatDate As String
' 0 = m-d-y
' 1 = d-m-y
' 2 = y-m-d
If Application.International(xlDateOrder) = 0 Then
SystemDateType = 0
SysFormatDate = "mm/dd/yyyy"
ElseIf Application.International(xlDateOrder) = 1 Then
SystemDateType = 1
SysFormatDate = "dd/mm/yyyy"
ElseIf Application.International(xlDateOrder) = 2 Then
SystemDateType = 2
SysFormatDate = "yyyy/mm/dd"
End If
'Of course you can do this:
'SystemDateType = Application.International(xlDateOrder)
'Or just use a Select Case...
'But just want to be clear and set the variable SysFormatDate
Dim StopedByUser As String: StopedByUser = "StopedByUser"
'Here you can define your own message.
Dim d1 As Date
'Look for the DateSerial function
Dim d2 As Variant
'This could be Long, but just want to set a data type
Dim ErrHandler As Integer
Dim n As Variant
'Or maybe String?
Dim entered_date As Date
'alwayes respect Data Types...
RetryInput:
n = Application.InputBox("Enter a date in " & SysFormatDate & " format: ")
'The user input...
'Cancel...
If n = False Then
MsgBox StopedByUser
End
End If
'Error Handler!
If IsDate(n) Then 'If the user input a real date...
entered_date = CDate(entered_date)
d1 = DateSerial(2011, 11, 16) ' ==> YYYY, MM, DD
'It is always better to use DateSerial!
'And hard coded... Well hope is just your for the question.
'd1 = 5 / 11 / 2021
If d1 >= entered_date Then
'The >= and <= are better in this case...
d2 = DateDiff("D", d1, entered_date)
MsgBox ("late by " & Abs(d2)) 'Abs return the absolute value, instead -321 return 321.
Else
MsgBox ("on time")
End If
Else 'If the user don't know what is a date...
'ask the user... want to try again...
ErrHandler = MsgBox("Please enter a formated date (" & SysFormatDate & ")", vbDefaultButton1 + vbYesNo, "Retry")
If ErrHandler = 7 Then '7 is NO
MsgBox StopedByUser
End
ElseIf ErrHandler = 6 Then '6 is YES and go back in the code to...
GoTo RetryInput
End If
'Check for MSGBOX here: https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/msgbox-function
End If
End Sub

Related

Evaluating user input decimal variable to range of 1 to 24 or error

Being new to VB I am trying to wrap my thoughts around validating a user input (in textbox) range of 1 to 24. I know this is probably a simple expression but my mind is on a Java expression rather than Visual Basic.
Private Sub HoursAppUsed_TextChanged(sender As Object, e As TextChangedEventArgs) Handles HoursAppUsed.TextChanged
'must check if string is numeric/integer or not'
Dim hoursEntered As String = HoursAppUsed.Text
'And hoursEntered > 0 Or hoursEntered < 25 ???? '
If IsNumeric(hoursEntered) Then
Dim decFromString1 As Decimal = Decimal.Parse(hoursEntered)
hoursEntered = "Value: " + hoursEntered
LabelFour.Content = hoursEntered
Else
LabelFour.Content = "Value is not Numeric!"
End If
'hoursEntered = "Hours Entered: " + hoursEntered'
'LabelFour.Content = hoursEntered'
End Sub
This could be easily achieved with a NumericUpDown-control by setting the Minimum and Maximum properties.
If you still want to use the TextBox instead, following should work:
Dim hoursEntered As String
Dim decFromString1 As Decimal
If Decimal.TryParse(hoursEntered, decFromString1) AndAlso
decFromString1 >= 1 AndAlso
decFromString1 <= 24 Then
hoursEntered = "Value: " + hoursEntered
LabelFour.Content = hoursEntered
Else
LabelFour.Content = "Value is not Numeric!"
End If

Conditional formatting of DataGridView cell data - Change color on negative

I was hoping to be able to use color based conditional formatting in the DefaultCellStyle.Format field for DataGridView cells, in a similar way to how Excel handles this.
For example in Excel, a format string of £#,##0.00;[Red]-£#,##0.00 will display negative values in red.
Is this supported in VB.NET ?
I am aware I can use the .CellFormatting event to conditionally change cell text color but was looking for a less bulky and restrictive way of doing this.
By creating the following CellFormatting addition, I am able to use Excel style conditional colour formatting in the cells format field. Setting the colour for negative/positive/zero values is supported.
Format string is expected to be in the following format (all colours optional) :
[colour]<format for +value> ; [colour]<format for -value> ; [colour]<format for zero value>
..a test DGV column with conditional formatting
c = New DataGridViewColumn
c.Name = "AmountOUT"
c.DataPropertyName = c.Name
c.HeaderText = "AmountOUT"
c.CellTemplate = New DataGridViewTextBoxCell
c.DefaultCellStyle.Format = "[Green]£0.00;[Red]-£0.00;[Blue]zero"
.Columns.Add(c)
..
Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
'Split format string to positive / negative / zero components
Dim posnegzero As List(Of String)
posnegzero = e.CellStyle.Format.Split(CChar(";")).ToList
Dim coloursPNZ As New List(Of String)
Dim remainderformatPNZ As String = ""
For Each s As String In posnegzero
If s.Contains("[") And s.Contains("]") Then
'Extract [xxx] contents
coloursPNZ.Add(s.Substring(s.IndexOf("[") + 1, s.IndexOf("]") - s.IndexOf("[") - 1))
'Append rebuilt format excluding [xxx]
remainderformatPNZ &= s.Substring(0, s.IndexOf("[")) & s.Substring(s.IndexOf("]") + 1, s.Length - s.IndexOf("]") - 1) & ";"
Else
coloursPNZ.Add("")
remainderformatPNZ &= s & ";"
End If
Next
'Set format excluding any [xxx] components
e.CellStyle.Format = remainderformatPNZ
'Check for positive value
If Val(e.Value) > 0 And coloursPNZ.Count >= 1 Then
If coloursPNZ(0) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(0))
End If
End If
'Check for negative value
If Val(e.Value) < 0 And coloursPNZ.Count >= 2 Then
If coloursPNZ(1) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(1))
End If
End If
'Check for zero value
If Val(e.Value) = 0 And coloursPNZ.Count >= 3 Then
If coloursPNZ(2) <> "" Then
e.CellStyle.ForeColor = Color.FromName(coloursPNZ(2))
End If
End If
End Sub
Dim dgv As DataGridView = Me.DataGridView1
For i As Integer = 0 To dgv.Rows.Count - 1
For ColNo As Integer = 4 To 7 ' number columns
If Not dgv.Rows(i).Cells(ColNo).Value < 0 Then
dgv.Rows(i).Cells(ColNo).Style.BackColor = vbcolor.Red
End If
Next
Next
checking for negative values lookout for strings format and check accordingly
Tryparse will convert the input to an integer if it succeeds - you don't need both the comps and value variables. Here's an example of how it works:
Dim comps As Integer
Dim input As String = "im not an integer"
Dim input2 As String = "2"
'tryparse fails, doesn't get into comps < 0 comparison
If Integer.TryParse(input, comps) Then
If comps < 0 Then
'do something
End If
Else
'I'm not an integer!
End If
'tryparse works, goes into comps < 0 comparison
If Integer.TryParse(input2, comps) Then
If comps < 0 Then
'do something
End If
End If

VBA: How can I tokenize an input in a cell

What I want to do is to convert time inputs in a cell to a specific format. Example:
"9" or "9 am" = 9:00:00 AM, which is the same as TIME(9, 0, 0)
"9 30" = 9:30:00 AM = TIME(9, 30, 0)
"4 30 pm" = 4:30:00 PM = TIME(16, 30, 0)
How can I achieve this in VBA?
Just a side note, this is actually my first time trying VBA.
Thanks.
I can support some learning:
Function timm(str As String) As Double
Dim spltstr() As String
Dim hr As Integer
Dim min As Integer
Dim sec As Integer
hr = 0
min = 0
sec = 0
str = Replace(str, """", "")
spltstr = Split(str)
hr = spltstr(0)
If UCase(spltstr(UBound(spltstr))) = "PM" Then hr = hr + 12
If 1 <= UBound(spltstr) Then
If IsNumeric(spltstr(1)) Then min = spltstr(1)
End If
timm = TimeSerial(hr, min, sec)
End Function
Place this in a module attached to the workbook. It can then be called as a function directly on the worksheet or from another sub.
It will change the text to a number so you will still need to apply a custom number format to the cell if using this as a UDF.
As per you comments to do it in place then you would use a Worksheet_Change event.
First use a custom format on the whole column that you desire like hh:mm:ss AM/PM. The put this in the worksheet code.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo getout
Application.EnableEvents = False
If Not Intersect(Range("A:A"), Target) Is Nothing Then
If Not IsDate(Target.Value) Then
Target = timm(Target.Value)
End If
End If
Application.EnableEvents = True
Exit Sub
getout:
Application.EnableEvents = True
End Sub
It calls the prior code and returns the number. So as you leave edit mode it changes it to a time.
It looks like you want to use Split and TimeSerial. Here's an example to get you started.
Public Function ConvertToTime(ByVal Input_ As String) As Date
Dim vaSplit As Variant
Dim lHour As Long, lMinute As Long
'split the string into an array using space as a delimiter
vaSplit = Split(Input_, Space(1))
'The first element is the hour
lHour = vaSplit(0)
'If there's more than one element, the second element is the minute
If UBound(vaSplit) > 0 Then lMinute = vaSplit(1)
'if the last element is "pm", then add 12 to the hour
If vaSplit(UBound(vaSplit)) = "pm" Then lHour = lHour + 12
ConvertToTime = TimeSerial(lHour, lMinute, 0)
End Function

vba type mismatch on my script

So, I'm getting a type mismatch in the VBA script of a Word document, however there isn't any line signaled on the editor... Can any of you give me an hint of what it might be?
Private Sub bt_run_Click()
'set months array
Dim months As Variable
months = Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
With ThisDocument.Tables(0)
Do While .Rows.Count > 2
.Rows(2).Delete
Loop
'Ask for year
Dim req As String
Dim yr As Integer
req = InputBox("Insere ano.")
If IsNumeric(req) Then
yr = CInt(req)
Else
MsgBox ("Erro")
Return
End If
'get previous year last week
'TODO
'Now generate current year months
For i = 1 To 12
'get number of mondays on the month (how many weeks belong here)
Dim mondays As Integer
mondays = MondaysOnMonth(i, yr)
'now generate a line for each monday
For k = 1 To mondays
.Rows.Add
Next k
Next i
'get next year first week
'TODO
End With
End Sub
Function MondaysOnMonth(ByVal month As Integer, ByVal year As Integer) As Integer
Dim mondays As Integer
mondays = 0
Dim d As Date
Dim dtStr As String
dtStr = "1/" & month & "/" & year
d = DateValue(dtStr)
Dim days As Integer
days = dhDaysInMonth(d)
For i = 1 To days
dtStr = i & "/" & month & "/" & year
d = DateValue(dtStr)
Dim w As Integer
w = Weekday(d, vbMonday)
If w = 0 Then
mondays = mondays + 1
End If
Next i
MondaysOnMonth = mondays
End Function
Function dhDaysInMonth(Optional ByVal dtmDate As Date = 0) As Integer
' Return the number of days in the specified month.
If dtmDate = 0 Then
' Did the caller pass in a date? If not, use
' the current date.
dtmDate = Date
End If
dhDaysInMonth = DateSerial(year(dtmDate), _
month(dtmDate) + 1, 1) - _
DateSerial(year(dtmDate), month(dtmDate), 1)
End Function
This pretty much generates how many lines as there're mondays in the entire year in the only table of the document.
I'm not really experienced in all this thing of Visual Basic for Applications, but I'm assuming it's some type casting that the compiler can't execute, however, I can't really see what it might be (and the compiler isn't giving me the necessary help), so what might it be?
In my (limited) experience, arrays are setup a little differently in VBA:
'set months array
Dim months(11) As String
months(0) = "Janeiro"
months(1) = "Fevereiro"
months(2) = "Março"
months(3) = "Abril"
months(4) = "Maio"
months(5) = "Junho"
months(6) = "Julho"
months(7) = "Agosto"
months(8) = "Setembro"
months(9) = "Outubro"
months(10) = "Novembro"
months(11) = "Dezembro"
Also, I could not address table 0, so changed it to table 1 and the code seemed to execute.
With ThisDocument.Tables(1)
Hope this helps!
Array Function
Returns a Variant containing an array!!!
Dim months As Variant
You were so close.
In your original code, you should be able to change Variable to Variant and the initialization will work as you expected.
Here I've copy/pasted your array initialization, swapped in Variant, and written a loop that confirms the array was properly initialized by printing the values to the Immediate Window (press Ctrl+G to view if it is not already visible):
Sub TestMonthArrayInitialization()
Dim months As Variant
months = Array("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
Dim i As Integer
For i = 0 To 11
Debug.Print months(i)
Next i
End Sub

Monthview Bolding Every Value

I created a Monthview and TimePicker in a form. I want the user to pick the time, and select a month which will bold the value selected each time, then select OK which will insert the value. I have all of this working fine. The issue is that if a user selects a date, then selects another date, or another date, all the dates are getting Bolded. I want the BOLD to only follow each most recent click.. if that makes sense, so that the user knows what value he chose.
Here is my click code:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim x As Date
x = MonthView1.value
MonthView1.DayBold(x) = True ' Bold the date
End Sub
What method do I need? Is there some kind of most-recent clicked property?
Try the following code:
Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
Dim x As Date
Dim MaxDate As Date
Dim MinDate As Date
MinDate = DateSerial(Year(DateClicked), Month(DateClicked), 1) 'Get first date of current month based on clicked date
MaxDate = DateSerial(Year(DateClicked), Month(DateClicked) + 1, 0) 'Get last date of current month based on clicked date
x = ActiveCell.Value 'Retreive value of last Bold date
If x >= MinDate And x <= MaxDate Then 'If last Bold date is in the current month then unbold it
MonthView1.DayBold(x) = False
End If
MonthView1.DayBold(DateClicked) = True 'Bold the clicked date
ActiveCell.Value = DateClicked 'Store current date in a sheet
End Sub
The idea is to save the Bold date in a sheet (you may hide it if you wish) and retrieve it when another date is selected. The Bold formatting is removed from the previous date and applied to the current one.
a bit more on bolding in a MonthView
'<code>
Private Sub CommandButton1_Click()
Dim Ss(50) As Boolean
'put in module DaysToBold$ and DatesToBold$ as public variables
DaysToBold = "713" ' sat Sun Tue
' DaysToBold = "" ' for none
DatesToBold = "x,1,2,12,22,30"
' DatesToBold = "x" ' if none
MonthView21_GetDayBold Now, 49, Ss
End Sub
Private Sub MonthView21_GetDayBold(ByVal StartDate As Date, _
ByVal Count As Integer, State() As Boolean)
Dim I As Integer, Mi%, DTB, DI%, DAd%
On Error Resume Next ' seem to be negative indices into State() even if declared ( -10 to 50)
For Mi = 1 To Len(DaysToBold) ' mark of the Days of week to bold
I = Mid(DaysToBold, Mi, 1) ' single digits 1 ..7 excel translate to integer
While I < Count
State(I - MonthView21.StartOfWeek) = True
I = I + 7 ' down the column
Wend
Next Mi
DTB = Split(DatesToBold, ",")
If UBound(DTB) > 0 Then
With MonthView21
For I = 1 To UBound(DTB) ' mark the date numbers to bold
DI = Weekday(DateSerial(Year(.Value), Month(.Value), 1), .StartOfWeek)
If DI = 1 Then DAd = 5 Else DAd = -2 ' 7 days of last month there if Di=1
State(DTB(I) + DI + DAd) = True
Next I
End With
End If
End Sub
'</code>