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.
Related
I am trying to set a combination of INDEX and MATCH formula results to a variable instead of set the results to a cell.
The code that I have so far is as below:
Dim todayDate as Date
Dim startDate as Date
todayDate = Format(Date, "MM/dd/yyyy")
startDate = "=INDEX([Financial_Calendar.xlsx]Calendar!C2,MATCH(CLng(todayDate.Value),[Financial_Calendar.xlsx]Calendar!C9,0))"
The startDate formula will find the todayDate inside the excel file and return the value of the startDate for todayDate row. The startDate value is in Date form.
But now I still did not able to get the value returned.
Can anyone help me on this ?
To start, you need to assign the date that you want to look up to todayDate to use in the worksheet function match.
for example, todayDate = ThisWorkbook.Sheets(1).Range("A1").Value, if you've stored the date in A1 of the first sheet of your workbook.
Next, if you want to use a worksheet function in VBA you need to specify that:
rowNumber = Application.WorksheetFunction.Match(arguments)
(https://learn.microsoft.com/en-us/office/vba/excel/Concepts/Events-WorksheetFunctions-Shapes/using-excel-worksheet-functions-in-visual-basic)
and the same for the index funciton, although it might be cleaner to use the .cells property in VBA directly. (https://learn.microsoft.com/en-us/office/vba/excel/concepts/cells-and-ranges/refer-to-cells-by-using-index-numbers)
hope this helps you get
I am reading data from a CSV file into a Sql table. Problem is some dates in a date column in the CSV would be empty. I am using the following code to convert the empty cells to NULL values. However I get the following error :Additional information: String was not recognized as a valid DateTime.Couldn't store in ANALISEDATUM Column. Expected type is DateTime
My code is :
Do Until parser.EndOfData = True
Dim data As String() = parser.ReadFields()
For i As Integer = 0 To data.Length - 1
If (String.IsNullOrEmpty(data(i))) Then
data(i) = DBNull.Value.ToString
End If
Next
datatabel.Rows.Add(data)
The error occurs at the last line.
Regards
I think, seems the format of the date adding to the table is wrong, please make sure the format of the table date column and csv file date value are same.
Ex: Table date format might be : mm/dd/yyyy
csv file date value format might be : dd/mm/yyyy
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 :)
I have created a DataGridView and I add columns to it like this:
Dim col_1 = New DataGridViewTextBoxColumn
col_1.Name = "Date"
col_1.DefaultCellStyle.Format = "MM/dd/yyyy"
data_grid.Columns.Add(col_1)
then I add data to the column like this:
data_grid.Item(1,1).Value = temp_date
The grid is filled with the correct data and everything works, however...when I click on the column headers, the column that shows the dates does not sort correctly (it sorts alphabetically) and I know this is because I set it up as "DataGridViewTextBoxColumn", but there is no option for a date type column. So how to I set it up as a date column so it sorts based on the date when the header is clicked?
Thanks.
You should also set the ValueType of the column:
DataGridView1.Columns(0).ValueType = GetType(Date)
Then convert date_temp to a Date-value before assigning this to the cell's Value.
Using CDate could be your first attempt:
data_grid.Item(1,1).Value = CDate(temp_date)
otherwise, investigate Parse, TryParse or Convert, to obtain the date-value.
I have an excel table and I want to extract a portion of the columns names: for example, the column name is "Y2014", I should get the String (or integer) "2014" only
here is my code
Function findValue(year As String)
Dim formattedYear As String
formattedYear = Microsoft.VisualBasic.Right(year, 1)
findValue = formattedYear
End Function
I get an error of value either if I try with a simple value doing findValue("Y2014") of if I try with the cell value findValue(Q1) (where A1 content is equivalent to "Tableau1[[#En-tĂȘtes];[Y2014]])":
can someone help? thanks.
First impression looking at your code is that functions always return a value so you have to specify what type the function returns like
Function findValue(year As String) as string
What sort of error are you getting?
I just had to get rid of the Microsoft.VisualBasic. mention