Excel VBA - the date format changes automatically - date

I'm trying to enter the Date value by adding a month to the date in Sheets("Sheet1").Cells(17, 3).Value which is 01/10/2011 but format as Oct-11. Then return in Sheets("Sheet1").Cells(17, 4).Value = LDate, to show Nov-11
sDate = Sheets("Sheet1").Cells(17, 3).Value --> this shows 01/10/2011 when I hove over sDate
LDate = DateAdd("m", 1, sDate) --> this shows 01/11/2011 when I hove over LDate
I then want to enter that value 01/11/2011 in to the following cell
Sheets("Sheet1").Cells(17, 4).Value = LDate
Selection.NumberFormat = "mmm-yy"
But in the cell it shows 11/01/2011 (Jan-11), why is it doing this and how can I fix this issue?

Minor issue
Selection.NumberFormat = "mmm-yy"
You have not selected anything so this format is placed wherever you left the cursor.
Major issue
You have hit the Excel bug that it will interpret dates as being in American (middle endian) format if it can when transferring data to a worksheet. "1/11/2011" would be "11 Jan 11" to an American so it is to Excel. "20/11/2011" is not a valid American date so to Excel it is "20 Nov 11".
I can duplicate your problem by declaring sDate and LDate as strings. DateAdd works correctly with strings so LDate is correct but when placed in a cell it is misinterpreted.
I can fix your problem by declaring sDate and LDate as dates:
Dim sdate As Date
Dim Ldate As Date

Selection.NumberFormat = "mmm-yy"
Above line is changing the number format. Actually When am running through your steps, I have come across the same issue.
But Number format is doing all this auto change.
Example:
While am running the macro, assume that selected cell is (17,3).
Before running macro: cell value is 01/11/2012
After running macro: cell value will be changed/formatted as "Jan-12". (Since number format is applied as "mmm-yy", so it consider as 01/11/2012 is consider as Jan-12)
Second time, if you select the cell (17,4)
Before running macro: cell value is 2/11/2012
After running macro: cell value will be changed / formatted as "Feb-12"

Instead of trying
Cells(row2,col2) = Cells(row1,col1)
Try this
Cells(row1,col1).Copy
Cells(row2,col2).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Related

How do I access a date picker user selected value, from within a word document, to use in in the same document using VBA?

I need to add Excel style automatic date creation, based on a user input in Word. After accessing development menu I can insert a date picker menu control from the dev menu. This provides a simple and easy to enter date format. I then wish to take the user selected value and add additional dates based on that date selection, increased by a number of days, and have these dates automatically appear into another area of the document. I have not been successful in searching online. Does anyone know of a solution to this?
For that, you'll need a ContentControlOnExit macro coded along the lines of:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim DtVal As Date, Fmt As String
With CCtrl
If .Type = wdContentControlDate Then
Fmt = .DateDisplayFormat
.DateDisplayFormat = "MMM-DD-YYYY"
DtVal = CDate(.Range.Text)
.DateDisplayFormat = Fmt
With ActiveDocument
.SelectContentControlsByTitle("Date1")(1).Range.Text = Format(DtVal + 5, "MMM D, YYYY")
.SelectContentControlsByTitle("Date2")(1).Range.Text = Format(DtVal + 21, "MMM D, YYYY")
.SelectContentControlsByTitle("Date3")(1).Range.Text = Format(DtVal + 90, "MMM D, YYYY")
End With
End If
End With
End Sub
and inserted into the 'ThisDocument' code module of the document or its template, where the output text content controls are titled Date1, Date2, Date3, etc.

Trying to build Expression for Table field to sort text dates, some with missing elements

Hi I am a newbie and have a problem I have been trying to solve for weeks. I have a table imported from excel with dates in text format (because dates go back to 1700s) Most are in the format "mmmyyyy", so it is relatively easy to add "1" to the date, convert to date format, and sort in correct date order. The problem I have is that some of the dates in the table are simply "yyyy", and some are empty. I cannot find an expression that works to convert these last two to eg 1 Jan yyyy and 1 Jan 1000 within the same expression. Is this possible, or would I need to do this in two queries? Sorry if this question is very basic - I cannot find an answer anywhere.
TIA
You can do something like:
Public Function ConvertDate(Byval Expression As Variant) As Date
Dim Result As Date
If IsNull(Expression) Then
Result = DateSerial(1000, 1, 1)
ElseIf Len(Expression) = 4 Then
Result = DateSerial(Expression, 1, 1)
Else
Result = DateValue(Right(Expression, 4) & "/" & Left(Expression, 3) & "/1")
End If
ConvertDate = Result
End Function

Updating textbox fill color using DocumentFormat.OpenXml from VB.Net

I have a request where I need to update a textbox fill color and border where specific text is found.
the following is part of my code.
For Each shape As DocumentFormat.OpenXml.Wordprocessing.Drawing In mainPart.Document.Body.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Drawing)()
'Replace date stamp with current date
'Find all placeholders that have mm/dd/yyyy as text.
If shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).Count() > 0 Then
If shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).First().InnerText = "mm/dd/yyyy" Then
LogVerboseMessage("Updating all date placeholders within document to current date")
Dim curDate As Date = Date.Now()
Dim curDateOnly As Date = curDate.Date
LogVerboseMessage("Shape has been found! Prepare to replace date.")
shape.Descendants.OfType(Of DocumentFormat.OpenXml.Wordprocessing.Text).First().Text = curDateOnly.ToString()
Dim color As String = shape.Descendants.OfType(Of DocumentFormat.OpenXml.Drawing.SolidFill).First.RgbColorModelHex.Val.ToString()
End If End If Next
When I run the above code in debug mode I get
"Sequence contains no elements"
error message.
I need to be able to get the current value and update it to a new value. Anyone have any suggestions?

Why is JXL giving me a date from previous day?

I'm reading an Excel spreadsheet using JXL and Groovy like this:
WorkbookSettings settings = new WorkbookSettings();
settings.encoding = "Cp1252"
settings.locale = new Locale("pt", "BR")
Workbook workbook = Workbook.getWorkbook(is, settings)
Sheet sheet = workbook.getSheet(0)
And then I have a cell in Excel which value is 09/01/2013 (dd/mm/yyyy). But then, when I retrieve cell contents, JXL automatically does some conversion and gives this back at me:
"09/01/13" == sheet.getCell(col, line).contents?.trim()
But then, when I cast the Cell to a DateCell, the Date representation of "09/01/13" becomes Jan 8, 2013 (!):
DateCell dc = ((DateCell) sheet.getCell(col, line))
println "date from JXL: ${dc.date}" // prints Tue Jan 08 22:00:00 BRST 2013
Would anyone have any ideas about how to fix this? If I could just retrieve the actual cell contents directly (09/01/2013), then I could do all the conversion stuff by myself.
Thanks!
From: http://www.andykhan.com/jexcelapi/tutorial.html#dates
When displaying dates, the java.util package automatically adjusts for the local timezone. This can cause problems when displaying dates within an application, as the dates look as if they are exactly one day previous to that which is stored in the Excel spreadsheet, although this is not in fact the case.
...
The easiest way to work around this (and the method used internally by the getContents() method of a jxl.DateCell) is to force the timezone of the date format as follows:
TimeZone gmtZone = TimeZone.getTimeZone("GMT");
SimpleDateFormat format = new SimpleDateFormat("dd MMM yyyy");
format.setTimeZone(gmtZone);
DateCell dateCell = ....
String dateString = format.format(dateCell.getDate());

GWT DateTimeFormat returns the wrong date

I have the following code
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
And the alert box shows the date
09/04/2014
instead of
21/04/2013
Why?
As others already say, it's because of your pattern. What they don't say is why it behaves that way.
When parsing 21/04/2013 as MM/dd/yyyy, DateTimeFormat will decompose the date as:
Month Day of month Year
21 4 2013
and it'll then adjust things to make a valid date. To do that, the Month part is truncated at 12 (so that temporary date is Dec 4th, 2013) and the remainder (21 - 12 = 9) is then added, leading to Sept. 4th 2014, which according to your format displays as 09/04/2014.
You wanted to show 21/04/2013 but the format was MM/dd/yyyy.
It should be dd/MM/yyyy
So change it like this:
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
You're reversing day and month.
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));