If you are currently using File > Open
to open the CSV file then try Data > Import External Data > Import Data
instead. This brings up the text import wizard which might give you more flexibility in how to import the file. Specifically, it lets you declare a column in the file as being text so that Excel does not try to parse the value
As Simon has explained, your terminology for the current date format is not correct. 9-FEB
corresponds to d-mmm
format and 02-19-2009
(NB deliberately changed to an unambiguous date for this example; 02-09-2009 is 9th Feb in the US but 2nd Sept in the UK) corresponds to mm-dd-yyyy
If you wanted to change the NumberFormat
to text for the range starting in cell B11 and ending in cell IV11 then you would use:
Range("B11:IV11").NumberFormat = "@"
@ signifies text format and you need the : operator to indicate a contiguous range of cells. Your original example of Range("B11","IV11")
actually indicates the union of cells B11 and IV11 and thus would only have affected two cells
It is unusual to have your data structured in rows rather than columns. Rather than having your dates in row 11, it would be more common to have them in column K instead so that you would use Range("K2:K65535")
or just Rows("K")
Most of the built-in Excel stuff like Sort and AutoFilter assume that your data is laid out in columns anyway. Row 1 traditionally contains the column names and then each subsequent row contains an individual record. You might want to look at how your CSV file is being generated to see if you can switich it to a more usable column-based structure
If you have a date like 19-FEB
as text in a cell then, assuming that you always want the year part to be the current year, you can change it to the first day of the month in mm/dd/yyyy
format in VBA. This example only changes one cell but you can use it as the basis for a wider solution:
Option Explicit
Sub main()
Dim strOrigValue As String
With ThisWorkbook.Worksheets("Sheet1").Range("B11")
strOrigValue = .Value
.NumberFormat = "mm/dd/yyyy"
.Value = DateSerial(Year(Date), Month(strOrigValue), 1)
End With
End Sub
DateSerial
creates a date serial number from the given year, month and day. Year(Date)
extracts the year part from the current system date, Month(strOrigValue)
takes the month part from the data that's already in the cell and we want the first day of the month