Formating a date as text in a macro

DavidSCowan

Board Regular
Joined
Jun 7, 2009
Messages
73
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
 

alan.m

Board Regular
Joined
Oct 22, 2010
Messages
120
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
 

snb_

Well-known Member
Joined
Nov 9, 2009
Messages
567
Code:
Sub snb()
  [A1].NumberFormat = "@"
  [A1] = "12-01"
End Sub
 

Forum statistics

Threads
1,081,753
Messages
5,361,093
Members
400,613
Latest member
Markdc123

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top