I'm trying to highlight all the rows in the datagridview which date is more than five days from the date today.
Here is my code.
For Each row As DataGridViewRow In dgv_Transfer_ledger.Rows
Dim now As DateTime = Date.Now
Dim delayDate = Date.Parse(row.Cells(0).Value.ToString)
Dim fiveDaysbefore = delayDate.AddDays(-5)
If now > fiveDaysbefore AndAlso now < delayDate Then
row.DefaultCellStyle.BackColor = Color.Yellow
ElseIf now > delayDate Then
row.DefaultCellStyle.BackColor = Color.Red
End If
Next
I'm getting the error String was not recognized as a valid DateTime.
Why I'm getting this error? Can anyone help me please? Thanks in Advance!
is your date in cell showing the correct format?
as they said, you need to check your cell's data and make sure it is in the correct format before they can actually run your code
if you know the format you may try
EG. (DD/MM/YY) (MM/DD/YY) [or something like this]
Dim delayDate = Date.ParseExact
I already got the answer.
Dim currentdate As Date = Date.Now.ToString("d")
You have to format the date based on the datatype in your datagridview.
I didn't use parse or ParseExact for getting the delaydate. Just make sure that the cell format in datagridview is the same format of the current date :)
Thanks for the reference guys :)
Related
I am trying to reference a file that has the date of the previous Friday at the end in the form of mm.dd.yy.
I need to now take that date and add it to the end of a string, to end of a string in order to open select the other workbook. This is what I have right now.
File Name:
Submittals Wk Ending 06.02.17.xlsx
This is what I have so far
Dim wrbk As String
Dim weekdate As String
range("a1").value="=TODAY()-WEEKDAY(TODAY())-1"
weekdate = Range("a1").Value
'range("b1").value="06.02.17"
'weekdate = Range("b1").Value
msgbox weekdate 'use to check what the date format is
wrbk = "Submittals Wk Ending " & weekdate
Windows(wrbk & ".xlsx").Activate
When I read it from B2 with the typed in format of 06.02.17 it works, however no matter what I do, I cannot get it to read it from A1 because it changes the format to m/d/yyyy. I have tried to copy it and paste as value. Nothing seems to work.
I have the other workbook open as well when I try to run it.
Any ideas? Thanks!
To get the previous Friday of any date, try below UDF. This should work fine if the Date NumberFormat is same as your System's Date format. The key is the CDate() which converts according to System's Date format which Office apps defaults to.
Option Explicit
Function GetLastFridayDate(AnyDate As Variant) As Date
Dim dInput As Date, dLastFriday As Date
dInput = CDate(AnyDate)
dLastFriday = dInput - Weekday(dInput) + vbFriday - IIf(Weekday(dInput) > vbFriday, 0, 7)
GetLastFridayDate = dLastFriday
End Function
Try
Range("A1").Value = Format$(Date - Weekday(Date) - 1, "MM.DD.YY")
I try to get a value from excel and to use in vb.bet but when i try to convert date from Excel my vba display error.
I try this:
Dim Target As DateTime = Nothing
Target = DateTime.ParseExact(currentWorksheet.Cells(CurrentRow, 19).Text, "dd-MMM-yy", System.Globalization.CultureInfo.InvariantCulture)
This code currentWorksheet.Cells(CurrentRow, 19).Text get me 3939344687 value from Excel, my date in excel are 01/26/17.
When i try to convert this number to date i get error.
Another code what i try are :
Target = Convert.ToDateTime(currentWorksheet.Cells(CurrentRow, 19).Text)
But doesn't work.
I am trying to pull all data entries that are within a userform selected month and year. I can get the code to run fine when I hard code the year but I want the year to come off of a text box. I converted the Textbox value to an integer using Cint() and dim'd it to "Year" in my if statement. I can get it to work if I write Cdate("3/1/2016"), but I want see if there is a way to run it like: Cdate("3/1/Year"). I tried it this way and get a typematch error on the Cdate Im pretty new to VBA so excuse my stupidity.
Ignore the "Month" variable I was just using that to put a stop on the code and step it through to see if it would enter my if statement.
Thanks in advance.
My Code
Private Sub OKBtn_Click()
Dim Sales As Range
Dim Year As Integer
Dim Month As Integer
Dim i As Integer
Year = CInt(YearText.Value)
Set Sales = Worksheets("Sales").Range("A4")
i = 0
If Sales.Offset(i, 1).Value >= CDate("3/1/2016") And Sales.Offset(i, 1).Value <= CDate(" 3/31/2016 ") Then
Month = 1
End If
In order for the CDate to work, you need to seperate the stings inside the brackets to 2 parts
1.The constant, in your case "3/1/".
2.And the variable, CInt(YearText.Value).
Option Explicit
Private Sub OKBtn_Click()
Dim DDate As Date
DDate = CDate("3/1/" & CInt(YearText.Value))
' for debug only
MsgBox "Date entered is :" & DDate
End Sub
I have 2 integers and 1 String. Now I want to convert it in date objects and display it in datepicker1 (I set its format to "short")
for example:
Dim Day As Integer = 14
Dim Month As String = "July"
Dim Year As Integer = 2016
I am expecting an answer as
7/14/2016 'this must be displayed in datepicker1
I searched through the internet and i found the same question here but this is from different language and i want it in VB. if my question has already been asked before. please post the link.
I am new to vb so i am hoping for your help guys!
Here's a way that uses Date.ParseExact:
Dim dt = Date.ParseExact($"{Year}-{month}-{day}", "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Note that i'm using string interpolation at $"..." which is available in VS 2015/VB14. Otherwise you could use String.Format:
Dim dtStr = String.Format("{0}-{1}-{2}", Year, month, day)
Dim dt = Date.ParseExact(dtStr, "yyyy-MMMM-dd", DateTimeFormatInfo.InvariantInfo)
Here you'll find more informations about the custom date and time format strings.
I AM FACING ONE PROBLEM.
I AM USING BELOW METHOD FOR MAKING STRING VALUE TO DATE.
string values is like this ="01/12/2002" like "dd/MM/YYYY"
My problem is that.
two string values
->1)01/01/2025
->2)1/1/2025
i am getting the Value like above 1 or 2
Dim d As Date = DateTime.ParseExact(dumm, "dd/MM/yyyy", Nothing)
if 1 comes nothing will happen but
if i get 2 i am facing error like String was not recognized as a valid DateTime.
As per my Analysts what i understood is date should be 2 digits Remaining all are two digits other wise giving the error.
but some times i am getting single digits from the excel to vb.net
how can i solve this issue...
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact(dumm, "d/M/yyyy", Nothing)
i put the break point on dumm ok dumm vlaues= "1/2/2026 12:00:00 Am"
Error...............
now
Dim dumm As String = DtSet3.Tables(0).Rows(k + 0).Item(3).ToString
Dim d As Date = DateTime.ParseExact("01/02/2026", "dd/MM/yyyy", Nothing)
Working Fine.......
Use the date format string "d/M/yyyy h:m:s tt". This will handle both cases i.e. with and without leading zero for the day and month. Additionally, since your actual variable has a time component in addition to the date, you need to add the format string for parsing time as well.
However, I would advise you to use TryParseExact, which will return boolean values based on success or failure of the parse rather than throwing exceptions.
Demo for using TryParseExact along with appropriate format string.