PC Review


Reply
Thread Tools Rate Thread

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

 
 
Saucer Man
Guest
Posts: n/a
 
      24th Dec 2008
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


--
Thanks!


 
Reply With Quote
 
 
 
 
ward376
Guest
Posts: n/a
 
      24th Dec 2008
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
 
Reply With Quote
 
Saucer Man
Guest
Posts: n/a
 
      29th Dec 2008
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.



"ward376" <(E-Mail Removed)> wrote in message
news:bd7940bc-b949-4cc7-8770-(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Saucer Man
Guest
Posts: n/a
 
      30th Dec 2008
I tried this but it is changing everything in column 1 to // including the
column header in row 1.


"Saucer Man" <(E-Mail Removed)> wrote in message
news:4958cd77$0$26446$(E-Mail Removed)...
> 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.
>
>
>
> "ward376" <(E-Mail Removed)> wrote in message
> news:bd7940bc-b949-4cc7-8770-(E-Mail Removed)...
>> 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
>>

>
>



 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      30th Dec 2008
You have to define the range... will the data you want to modify
always be in column a? Will there be any interruptions (blanks) in
column a?

http://support.microsoft.com/kb/291304/en-us

Cliff Edwards

 
Reply With Quote
 
Saucer Man
Guest
Posts: n/a
 
      30th Dec 2008
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.

"ward376" <(E-Mail Removed)> wrote in message
news:5bc2beee-3a17-4c00-a178-(E-Mail Removed)...
> You have to define the range... will the data you want to modify
> always be in column a? Will there be any interruptions (blanks) in
> column a?
>
> http://support.microsoft.com/kb/291304/en-us
>
> Cliff Edwards
>



 
Reply With Quote
 
ward376
Guest
Posts: n/a
 
      30th Dec 2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I apply my column format to all subfolders automatically? =?Utf-8?B?TWVsaXNzYQ==?= Microsoft Outlook Installation 6 5th Jul 2008 11:26 AM
Link imported contacts to imported accounts automatically Dools Microsoft Outlook BCM 0 3rd Jul 2008 09:55 AM
Can I apply my column format to all folders automatically Ido Microsoft Outlook Installation 2 22nd May 2008 11:44 PM
Format a column automatically? magic kat Microsoft Excel Discussion 2 17th Aug 2007 01:56 AM
How to automatically adapt column width with XML format? Francesco Sblendorio Microsoft Excel Programming 2 22nd Jun 2004 02:47 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:58 PM.