I've written an export to excel function in my program, which coincides with an import program too.
The import program is running some validation checks, but some of them are failing because the Excel sheet is formatting them as MM/dd/yyyy, however most of them are being stored as dd/MM/yyyy.
After looking in my exports program, I can see that in the grid, before exporting, the dates are all correctly formatted as dd/MM/yyyy, so the issue is not here.
When opening the exported Excel file, I can see that some of the dates are stored as MM/dd/yyyy, however.
The regional settings on my PC are correct, set to UK and even after checking the Excel format on the column, I can see it is set to dd/MM/yyyy.
So, what is there that could be going wrong? Why are some exported differently?
The values as seen in the grid (Correct format - See top 2 rows 'Rate One Start', 01/06/2016)
Code in the export routine
Dim formatRange As Excel.Range
formatRange = xlWorksheet.Range("F2", "F99000")
formatRange.NumberFormat = "dd/MM/yyyy"
formatRange = xlWorksheet.Range("I1", "I99000")
formatRange.NumberFormat = "dd/MM/yyyy"
formatRange = xlWorksheet.Range("J1", "J99000")
formatRange.NumberFormat = "dd/MM/yyyy"
formatRange = xlWorksheet.Range("M1", "M99000")
formatRange.NumberFormat = "dd/MM/yyyy"
formatRange = xlWorksheet.Range("N1", "N99000")
formatRange.NumberFormat = "dd/MM/yyyy"
formatRange = xlWorksheet.Range("Q1", "Q99000")
formatRange.NumberFormat = "dd/MM/yyyy"
For k As Integer = 1 To dgvExport.Columns.Count
xlWorksheet.Cells(1, k) = dgvExport.Columns(k - 1).HeaderText
Next
Dim eStr As String = ""
Dim nStr As String = ""
Me.Cursor = Cursors.WaitCursor
For i = 0 To dgvExport.RowCount - 1
For j = 0 To dgvExport.ColumnCount - 1
Try
eStr = Trim(dgvExport(j, i).Value)
nStr = eStr.Replace(vbCr, "").Replace(vbLf, "")
xlWorksheet.Cells(i + 2, j + 1) = nStr
Catch
xlWorksheet.Cells(i + 2, j + 1) = dgvExport(j, i).Value
End Try
Next
Next
The Excel file I exported (Notice some of the dates are dd/MM, whereas some are MM/dd)


1-jun-16vs6-Jan-16due to VBA converting your data according to US settings. Not sure aboutvb.netdate vs string. - Ron Rosenfeld