Excel VBA macro: Extract Date from String as dd/mm/yyyy - excel

I have an Excel sheet with a formatted cell as text with a timestamp from I need to extract the date in format dd/mm/yyyy and store the result in a cell as date. What I normally do on other calculations is get the result for first line, populate to bottom line, then copy and paste as values, not the formula itself. The file could have 30000 lines.
I have tried with the date function which works fine on the sheet, but not on the VBA code.
Any help are welcome
The Excel sheet look as the image

Excel will automatically recognize most dates. By only supplying the first 10 characters of the string, i.e. the date, Excel will know what to do with it.
Your formula could be simplified, into a cell with a dd/mm/yyyy format:
=DATEVALUE(LEFT(A1,10))
The corresponding VBA function would look like:
Format(Left(Range("A1").Value, 10), "dd/mm/yyyy")

Related

How to put todays date and time in excell cell

Within an Excel cell, I want to have the current (dynamic) date time printed.
I tried with following format:
TEXT(TODAY(); "ddmm");TEXT(TIME(HOUR(NOW());MINUTE(NOW());SECOND(NOW()));"mmss")
The format cell is not formatted as text. However, when I select the cell I have exactly that what is typed iso 07111533
I tried already with TEXT(TODAY(); "ddmm") and then my cell is filled with 0711, which is ok.
Expected result should be something like 07113815
Actual result:
TEXT(TODAY(); "ddmm");TEXT(TIME(HOUR(NOW());MINUTE(NOW());SECOND(NOW()));"mmss")
You're over-complicating:
=TEXT(NOW(),"ddmm") & TEXT(NOW(),"mmss")

Convert Date to Text without losing the format in Excel?

I have a column in Excel Sheet which contains all the dates in custom dd-mm-yyyy format. I need to convert all these dates to text without losing the format. If I am changing the cell format to Text, I am losing the format. I tried to copy the cell values as values but did not work. I searched a lot on the internet, but did not find any useful resource. What's the possible solution?
Try using the TEXT function.
=TEXT(A1,"dd-mm-yyyy")
Use this formula for keeping the long date format from "A1" cell in another cell (exp: "B1"):
=TEXT(A1,"[$-F800]dddd, mmmm dd, yyyy")
The cell "A1" is a cell contains a date (such as today date with «today()» formula) with long date format.
Also, you can use this VBA code for getting same format with specify font and size in print's header:
ActiveSheet.PageSetup.RightHeader = "&""Arial Rounded MT Bold,Regular""&16" & Range("B1").Value
This code will shows the "B1" cell (as a text from "A1" cell) in Right of Header in Print.

day and month are reversed

I have a cell with the following content:
01/02/2015
The cell is date formatted.
Then I copy the value and put it in my module class:
Set Line = New CTravelLine
Line.Date= Cells(1, 8).value
Everything works fine until the moment I put this value in another cell:
The value 01/02/2015 becomes 02/01/2015.
I am using this format (dd/mm/yyyy). I have the impression that when the days are numerically lower than the month, the 2 values are reversed. The values are reversed whatever the method I tried:
Method 1:
Dim WrdArray() As String, datetest As String
WrdArray() = Split(travelLine.Date, "/")
datetest= WrdArray(0) & "/" & WrdArray(1) & "/" & WrdArray(2)
Cells(5, 5) = datetest
Method 2:
Cells(5, 5) = travelLine.Date
I don't understand how I can solve this problem.
This might have happened due to 'Regional formatting problem'.
Excel has a habit of forcing the American date format (mm/dd/yyyy) when the dates have been imported from another data source. So, if the day in your date happens to be 1 - 12, then Excel will switch the date to mm/dd/yyyy.
When dates are imported from a text file, there is an option in the VBA code to apply regional format which corrects this problem.
OR
Change number format of date column in excelsheet from 'date' format category to 'text'; save it.
(After Saving run the VBA Code if you have any. Now check whether the date format is 'text' or changed back to 'date'.)
If it has changed back to 'date' try to fix it as 'text'
If it's 'text'; Correct the erroneous date cells and save the excel sheet. This will make dates not to change automatically to American Format.
Long story short, I had a similar problem where the dates are working just fine in some cells but keep flipping in others regardless if I copy paste or enter manually, I did the whole data text to column and cell formatting solutions and all of that didn't work.
The solution actually is not in excel, it's in the region and language setting.
To have the dates display as MM/DD/YYYY in the formats tab change the format to US.
To have the dates display as DD/MM/YYYY in the formats tab change the format to UK.
I had the same issue as you .
Let me explain what I want to do :
I have a csv file with some date.
I copy a range of my sheet in variable table. The range contain some columns with dates.
I make some manipulations on my table (very basic ones)
I transpose my variable table (since only the last dimension of a variable table can be increase)
I put my variable table on a new sheet.
What I found:
There is no date issue after executing step 1-4. The date issue shows up when writing on the sheet...
Considering what Avidan said on the post of Feb 24 '15 at 13:36, I guess it is excel which forces the American format mm/dd/yyyy... So I just change the date format at the very beginning of my program :
Before starting any manipulation date:
do
.Cells("where my date is") = Format(.Cells("where my date is"), "mm dd yy")
execute your program
write the table on a sheet
back up to the date format you like directly on the sheet
Just use:
Line.Date = cDate(Cells(1, 8).value2)

Dates Formatting in Excel

I have a column with many thousands of rows. The column is meant to be date and is of the format dd/mm/yyyy
But, when I try to do formulas based on the dates, something is clearly amiss.
For example, if you try to apply autofilter on the dates, some of them are grouped as a year with the expandable boxes while others appear as their own items.
For each record I tried a formula to parse it apart.=DATE(RIGHT(A2,4),MID(A2,4,2),LEFT(A2,2))
That did not help.
I also selected the column and switched it from general to date format
I really don't know how to ask the question any clearer. I can tell you that with a date of the format 1/11/2013 when I run =year(right(A1,4)) I get 1903 instead of 2013. When I run =date(right(A1,4),mid(A1,3,2),left(A1,2)) the formula returns 2/10/3192
It's very simple why your formulas doesn't work corectly. When yor're using somehting like this: RIGHT(A2,4), your value from A2 translates to 41579 for 1/11/2013 (Excel stores all dates as integers and all times as decimal fractions. You can read more here). Next formula should work well:
=DATE(RIGHT(TEXT(A2,"dd/mm/yyyy"),4),MID(TEXT(A2,"dd/mm/yyyy"),4,2),LEFT(TEXT(A2,"dd/mm/yyyy"),2))
Btw, if you'd like to get correct format for dates, you can add formula in some empty column (but before set date format for this column):
=A2*1
and drag it down. Then copy values from this temp column and paste them using "Paste special->Values" in colunm A (where should be date format as well)
Or you can use this simple macro:
Sub test()
With Range("A2:A100")
.NumberFormat = "dd/mm/yyyy"
.Value = .Value
End With
End Sub

Change text data to correct datatype

I'm populating an Excel sheet from DTS. The result is formatted as text since DTS sees the cells as varchar(255).
The cell-formatting is correct. The problem is that Excel is thinking of the data as text instead of datetime or numeric.
E.g. the value in a cell shows as "2009-01-01 00:00:00". If I press F2 to edit the cell, then press ENTER, Excel realises it's a date and then formats it according to the cell formatting.
How can I format the values as numeric or datetime with VBA?
Seems you can just assign the value of a cell or range to itself and that lets Excel determine the datatype. You can do this a row at a time, eg:
Worksheets("MySheet").Range("A:A").Value
= Worksheets("MySheet").Range("A:A").Value

Resources