How can I automatically format a column as it is imported?

S

Saucer Man

I have a macro which automatically imports the contents of a .csv file. The
data which is imported into Coulmn A is a date but it is not in the format
that we want. When it is imported, it looks like this...

20081223155307

We want it to look like this...

12/23/08

We currently have the column format set as Numeric without decimal places.
Can we set the column to a Date format and then have the macro automatically
truncate and convert the imported data to the format we want?

Here is my macro which does the import...

With
ActiveSheet.QueryTables.Add(Connection:="TEXT;c:\UPS_CSV_EXPORT.csv",Destination:=rDest)
.Name = "UPS_CSV_EXPORT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
 
W

ward376

Sub way()
Dim c As Range

For Each c In Sheet1.UsedRange.Columns(1) 'adjust range as required
c.Value = Mid(c.Text, 5, 2) & "/" & Mid(c.Text, 7, 2) & "/" & Mid
(c.Text, 3, 2)
Next c
Sheet1.UsedRange.Columns(1).NumberFormat = "mm/dd/yy;@"

End Sub


Cliff Edwards
 
S

Saucer Man

Cliff,

This loop will format just the imported rows? The sheet is cumulative and
will have rows in it that already have the date in column 1 formatted
properly.
 
S

Saucer Man

I tried this but it is changing everything in column 1 to // including the
column header in row 1.
 
S

Saucer Man

Thanks for that link. If I set Column A to be in Date format, when I do the
import from the .csv, the field looks like this...

##########

Knowing this is constant, I modified your script to check the data and if it
finds ##, then replace that data with the current date. This seems to work
nicely. Thanks.
 
W

ward376

You can use the named range produced when you add the query table to
define the range to loop within:

Sub way()
Dim c As Range
Dim bgn As Long
Dim nd As Long

For Each c In Sheet1.Range("UPS_CSV_EXPORT").Columns(1) 'adjust range
as required
c.Value = _
Mid(c.Text, 5, 2) & "/" & Mid(c.Text, 7, 2) & "/" & Mid
(c.Text, 3, 2)
Next c

Sheet1.Range("UPS_CSV_EXPORT").Columns(1).NumberFormat = "mm/dd/yy;@"

End Sub

Cliff Edwards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top