Problems with reformatting date in VBA

Yamezz

Active Member
Joined
Nov 22, 2006
Messages
367
Office Version
  1. 2019
I have a macro that uses the following bits of code:
VBA Code:
Dim ReportDate As Date, strReportDate As String
ReportDate = Date
strReportDate = Format(Date, "dd mmmm yyyy")

strReportDate evaluates to 14 December 2023 when I hover over it in the VBE
I then use strReportDate to enter into column N

VBA Code:
Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1) = strReportDate

But this results in 14-Dec-23 in the cell. Excel has automatically formatted it to d-mmm-yy. This is not what I need - I need the text of strReportDate (14 December 2023)
What am I doing wrong?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try
Rich (BB code):
Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1) = "'" & strReportDate
 
Upvote 0
Solution
Why do you want to enter your date as text ? By doing so you limit it usefullness.
You could just format the cell or column to show you the date in format you need without forcing it to be text.

Rich (BB code):
    Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1).Value2 = ReportDate
    Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1).NumberFormat = "dd mmmm yyyy"
 
Upvote 0
Why do you want to enter your date as text ? By doing so you limit it usefullness.
You could just format the cell or column to show you the date in format you need without forcing it to be text.

Rich (BB code):
    Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1).Value2 = ReportDate
    Xero.Sheets("Xero Sales Invoice").Range("N" & XeroRow + 1).NumberFormat = "dd mmmm yyyy"
The point of this macro is to prepare a .csv file for import into the Xero accounting software, which will be confused by "45274" when it's looking for "14 December 2023"
 
Upvote 0
Fair enough, thanks for clarifying.
If you create the csv file using Save As typically the number formatting you apply to cell will be reflected in the csv file. As such the issue is often the other way around ie that number formatting with commas or dollar signs or brackets has been applied and appear in the csv file.
But there are other ways of creating a csv file in which case the conversion to text may be helpful.
 
Upvote 1

Forum statistics

Threads
1,224,830
Messages
6,181,229
Members
453,026
Latest member
cknader

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