Formating a date as text in a macro

DavidSCowan

Board Regular
Joined
Jun 7, 2009
Messages
78
Could someone help me please.
Below is an example entry in a single cell. I am parsing the different elements out into separate columns.
16 Aug Payment to xxx Type of payment yyy Amount £zzz<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
The first stage is to extract the date and using Instr etc. I end up with the date in Column 1 and the rest of the string in Column 2:
16 Aug Payment to xxx Type of payment yyy Amount £zzz<o:p></o:p>
<o:p></o:p>
So far so good, but the problem is that Excel insists on date formatting the 16 Aug as date but I want it as text. Excel enters 16 Aug as 16-Aug i.e. putting in a hyphen (and underneath it has entered 16/08/2011). As it happens in my data the 16 Aug date is for another year but Excel, trying to be helpful, presumes it must be 2011 which it isn’t.
Could someone please tell me how in the macro I format the 16 Aug as text?
After the macro has run I can format the column as text using TEXT(A1,"ddmmm") etc. but how do I do it in the macro?<o:p></o:p>
I have a related question to this regarding the vba Intellisense and why it can’t give me guidance in this case. Why is it that when the full stop is inserted after EntireColumn in the line of code Range("A1:A1").EntireColumn. I am offered options. But when I type Range("A1:A1").text or Range("A1:A1").numberformat. I am not? Does anybody know?
Thank you in advance.

PS I am using Windows 7 and Office 2007
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Could someone help me please.
Below is an example entry in a single cell. I am parsing the different elements out into separate columns.
16 Aug Payment to xxx Type of payment yyy Amount £zzz<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
The first stage is to extract the date and using Instr etc. I end up with the date in Column 1 and the rest of the string in Column 2:
16 Aug Payment to xxx Type of payment yyy Amount £zzz<o:p></o:p>
<o:p></o:p>
So far so good, but the problem is that Excel insists on date formatting the 16 Aug as date but I want it as text. Excel enters 16 Aug as 16-Aug i.e. putting in a hyphen (and underneath it has entered 16/08/2011). As it happens in my data the 16 Aug date is for another year but Excel, trying to be helpful, presumes it must be 2011 which it isn’t.
Could someone please tell me how in the macro I format the 16 Aug as text?
After the macro has run I can format the column as text using TEXT(A1,"ddmmm") etc. but how do I do it in the macro?<o:p></o:p>
I have a related question to this regarding the vba Intellisense and why it can’t give me guidance in this case. Why is it that when the full stop is inserted after EntireColumn in the line of code Range("A1:A1").EntireColumn. I am offered options. But when I type Range("A1:A1").text or Range("A1:A1").numberformat. I am not? Does anybody know?
Thank you in advance.

PS I am using Windows 7 and Office 2007
Hi,
I'm sure there will be a much more elegant and reasonable way to do this, but a cheeky way is to add a really old fashioned text modifier to the output line. You see, in the old days we could put an apostrophe in front of something in a cell to define it as text, eg:
'12
will be text
so I suggested you concatenate an apostrophe to your output (did you say what your output line was?)
whatever = "'" & Mid(myInput, 1, mydatelength)

HTH

my whole code is

Code:
Sub strdate()
    Dim myInput As String
    Dim myDateStart As Integer
    Dim mydatelength As Integer
 
    myInput = Cells(22, 4).Value
    myDateStart = InStr(1, myInput, "Aug")
    mydatelength = myDateStart + 3
    Cells(22, 5) = "'" & Mid(myInput, 1, mydatelength)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,447
Members
448,966
Latest member
DannyC96

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