Can't get date formatting right when writing to worksheet

JONeill

Board Regular
Joined
Sep 2, 2018
Messages
58
I have a form that uses date picker controls for a beginning and ending date. I have a listbox that uses the rowsource property to display columns on the data. Whenever I write to the worksheet, the dates display as numbers. Even when I change the cell formatting to short date, then next write changes the format. Both date pickers are set to short date. The immediate window shows valid formats but not after writing to the cells. Here is some code.


The first set of code calls the module that actually writes the collection to the worksheet.
Code:
 Set mLog = Factory.CreateMaintLog(Trim(txtProjectName.Text), _
                                            cboLocation.Value, _
                                            dtStartDate.Value, _
                                            dtCompletionDate.Value, _
                                            chkCompleted.Value, _
                                            Trim(txtDescription.Text), _
                                            Trim(txtMaterials.Text))
                                            
'Add item to collection and write to Data worksheet.
colMaintLogData.Add mLog
IData.WriteMaintLogData maintLogParms, colMaintLogData



This code writes to the worksheet...



'Set first row
row = DRngPrms.firstRow
With Sheets(DRngPrms.ActiveDataSheet)
 'Delete current data in spreadsheet
 Range(DRngPrms.DataRange).Clear
 'Write data for each row
 For Each mLog In colData
  .cells(row, colNames.ProjectName).Value2 = mLog.ProjectName
  .cells(row, colNames.Location).Value2 = mLog.Location
  .cells(row, colNames.StartDate).Value2 = mLog.StartDate
  .cells(row, colNames.CompletionDate).Value2 = mLog.CompletionDate
  .cells(row, colNames.Completed).Value2 = mLog.Completed
  .cells(row, colNames.Description).Value2 = mLog.Description
  .cells(row, colNames.Materials).Value2 = mLog.Materials
  row = row + 1
 Next mLog
End With
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try:

Code:
[COLOR=#333333].cells(row, colNames.StartDate).[/COLOR][COLOR=#0000ff]Value [/COLOR][COLOR=#333333]= [/COLOR][COLOR=#0000ff]cdate[/COLOR][COLOR=#333333](mLog.StartDate)[/COLOR]
 
Upvote 0
Hello JONeill,

Try removing .Value2 from:-

Code:
.Cells(Row, colNames.StartDate).Value2 = mLog.StartDate

In brief, dates are stored as numbers so Value2 will find the underlying value of a cell. Value2 will not check the cell format and convert it to a date (as you would read it). Its pretty well much the same when using just Value. Just some minor differences.

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
Removing the Value2 seems to have done the trick. I had heard that Value2 was designed as a "patch", if you will, to Value and the preferred way was to use it. Seems work fine now. Thanks!
 
Upvote 0
You're welcome. I'm glad to have been able to help.

Cheerio,
vcoolio.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top