Computing.Net > Forums > Office Software > Exporting csv attachment to xls

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Exporting csv attachment to xls

Reply to Message Icon

Name: Ed
Date: February 3, 2009 at 06:42:04 Pacific
OS: Windows XP
Subcategory: Microsoft Office
Comment:

Good morning,

I receive an email overnight with an attached .csv file. I would like to automate the process of opening the file, copying the data and appending it to an existing .xls file.

The data can fall into one of three categories, and so the next step would be to check a column for the value and separate the pasting into different sheets.

I know I can figure out this last part on my own but the actual automated importing has stumped me.

Thanks for your help,

Ed



Sponsored Link
Ads by Google

Response Number 1
Name: swimfast64
Date: February 3, 2009 at 07:10:50 Pacific
Reply:

Jon? Mike? Have I stumped you guys? :)

Just kidding... any help is greatly appreciated

Thanks, Ed


0

Response Number 2
Name: jon_k
Date: February 3, 2009 at 07:35:05 Pacific
Reply:

I'll be honest, I'm rubbish at VBA in outlook. The excel bit sounds pretty easy, you're just looping through the file and depending on the value in column A copying to a different sheet. You can do this with a select case statement.

The way I'd play it is to save the csv in a fixed location, then open up your excel sheet, refresh your csv query, loop through it and copy / paste.

Something like (Note this is completely untested!)

sub getthatquery()

vars1max = sheet1.range("A65536").end(xlup).row +1 'assumes column A is populated in sheet 1
vars2max = sheet2.range("A65536").end(xlup).row + 1
vars3max = sheet3.range("A65536").end(xlup).row + 1
vars1text = "Sheet 1 Identifier"
vars2text = "Sheet 2 Identifier"
vars3text = "Sheet 3 Identifier"
varcsvlastrow = sheet4.range("A65536").end(xlup).row

    for irow = 2 to varcsvlastrow
    varcsvtext = sheet4.cells(irow, 1).value
    select case varcsvtext
    case vars1text
        sheet4.range(irow & ":" & irow).copy sheet1.range(vars1max & ":" & vars1max)
        vars1max = vars1max + 1
    case vars2text
        sheet4.range(irow & ":" & irow).copy sheet2.range(vars2max & ":" & vars2max)
        vars2max = vars2max + 1
    case vars3text
        sheet4.range(irow & ":" & irow).copy sheet3.range(vars3max & ":" & vars3max)
        vars3max = vars3max + 1
    case else
        sheet4.range(irow & ":" & irow).interior.colorindex = 4 'colours any row it can't import.
    end select
    next irow

end sub

Give it a try, hopefully you can work out the rest!


0

Response Number 3
Name: swimfast64
Date: February 3, 2009 at 09:54:09 Pacific
Reply:

Thanks, Jon.

That is a very good answer, I didn't consider some of the logic. Thank you very much.


0

Response Number 4
Name: DerbyDad03
Date: February 5, 2009 at 11:01:46 Pacific
Reply:

Just an FYI...

re: sheet1.range("A65536").end(xlup).row +1

Now that 2007 allows for more than 65536 rows, the new "standard" for finding the last row is:

Sheet1.Range("A" & Rows.Count).End(xlup).Row

This method is backward compatible with previous versions, so it should adopted going forward.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Exporting csv attachment to xls

Outlook - Can't Open CSV Attachment www.computing.net/answers/office/outlook-cant-open-csv-attachment/7542.html

Export Outlook email to Excel www.computing.net/answers/office/export-outlook-email-to-excel/8500.html

Export from Outlook to Excel www.computing.net/answers/office/export-from-outlook-to-excel/2204.html