Solved

Correct date format in macro

Posted on 2011-03-13
3
307 Views
Last Modified: 2012-05-11
Attached is a macro and sheet containing an output tab.  The macro output tab processes most of the dates correctly as they are formatted by an abbreviated month, followed by a four(4) digit year (i.e. Mar 2011).  This is precisely correct.  Some types of trades though have an actual date that is important also and so the formatting is not correct for those.  In the cases where there is an actual date in the Exp column (some of these are denoted in yellow), the format for them should be an abbreviate month, followed by a two (2) digit date, followed by a four (4) digit year (i.e. Mar 5 2011) with no commas or punctuation.  Assistance is greatly appreciated.  Both the macro and sheet are here.
Option Explicit

Public Sub CopyTradeHistory()
  Dim wks As Worksheet
  Dim rngSrc As Range
  Dim strSheetname As String
  Dim lastrow As Long
  
  On Error Resume Next
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  Set wks = ActiveSheet
  strSheetname = wks.Name
  Set rngSrc = wks.Cells.Find("Account Trade History")
  Set rngSrc = wks.Range(rngSrc, rngSrc.End(xlDown).End(xlDown).Offset(0, 11))
  ActiveWorkbook.Worksheets(strSheetname & "_output").Delete
  Set wks = ActiveWorkbook.Worksheets.Add
  wks.Name = strSheetname & "_output"

  rngSrc.Copy wks.Range("A1")
  wks.Columns(3).Insert
  wks.Columns(5).Insert
  wks.Cells(2, 3).Value = "Strategy#"
  wks.Cells(2, 4).Value = "Strategy"
  wks.Cells(2, 5).Value = "Leg#"
  wks.Rows("1:2").Font.Bold = True
  lastrow = wks.Range("a" & 64000).End(xlUp).Row
  wks.Range("E3:E" & lastrow).Formula = "=IF(OR(H3<>H2,E2=""leg2""),""Leg1"",""Leg2"")"
  wks.Range("E3:E" & lastrow).Value = wks.Range("E3:E" & lastrow).Value
  wks.Range("C3:C" & lastrow).Formula = "=counta(B3:B$3)"
  wks.Range("C3:C" & lastrow).NumberFormat = "General"
  wks.Range("C3:C" & lastrow).Value = wks.Range("C3:C" & lastrow).Value
  wks.Range("I3:I" & lastrow).NumberFormat = "mmm yyyy"
  wks.Columns(3).Insert
  wks.Cells(2, 3).Value = "Position#"
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
  Application.DisplayAlerts = True
End Sub

Open in new window

TradeHist-2.3b.xlsm
0
Comment
Question by:rtod2
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 35123687
rtod2,

>>the format for them should be an abbreviate month, followed by a two (2) digit date, followed by a four (4) digit year
>>(i.e. Mar 5 2011)


If you want a two-digit day, then shouldn't your example be:

Mar 05 yyyy

Try changing:

  wks.Range("I3:I" & lastrow).NumberFormat = "mmm yyyy"

Open in new window


to:

  wks.Range("I3:I" & lastrow).NumberFormat = "mmm dd yyyy"

Open in new window


Patrick
0
 

Author Comment

by:rtod2
ID: 35123751
Most of those cells I want in the "mmm yyyy" format but the ones that look like this >> http://screencast.com/t/Dy2FnVVX I would prefer to have in the "mmm dd yyyy" format.  I'm not sure how to differentiate.  Please have a look at the actual original input tab and macro if possible.
0
 

Author Comment

by:rtod2
ID: 35123981
I have re-examined this need and think it is a bad idea after all at this point.  Thanks for the assistance.  Here is the new question >> http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26883875.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open excel in 2016 is slow 4 21
Formula or Macro to determine variance 17 75
ADD New Entries 7 15
NEED TO UPDATE DATA IN EXCEL 18 27
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now