+ Reply to Thread
Results 1 to 10 of 10

Convert a CSV file into a formatted Excel file

  1. #1
    Registered User
    Join Date
    01-06-2009
    Location
    Brazil
    MS-Off Ver
    Excel 2003
    Posts
    4

    Convert a CSV file into a formatted Excel file

    Hello everyone!

    I tried many ways to convert a CSV file into a formatted Excel (.xls) file via VBA. I have a file with 5 lines (header included) and about 10 columns (delimited by commas). How can I format it via vba on button click action? Does anyone happen to know?

    Thanks everyone!

  2. #2
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    Hi

    Can't you just open it up in Excel, apply any formatting as required and then resave as an xls?

    Richard
    Richard Schollar
    Microsoft MVP - Excel

  3. #3
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    Have you tried recording a macro when you open the file manually, format as desired and then save.

    Give that a try to get all the syntax, etc. correct for the formatting you want to perform.

    HTH

  4. #4
    Registered User
    Join Date
    01-06-2009
    Location
    Brazil
    MS-Off Ver
    Excel 2003
    Posts
    4
    I have these two examples of codes to try to do it via VBA:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I tried to use them changing only the paths and the fields, but I get some errors. Mainly on the first code above, I get an error on the variable "rs = New ADODB.Recordset".

    I need to automate it because the users that will use this file don't know how to format it manually.

    Could you help me with that?

    Thanks
    Last edited by VBA Noob; 01-08-2009 at 05:08 PM. Reason: Added coded tags as per forum rules

  5. #5
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    How many records are held in the csv file? Since you are not filtering the data in any way, I think it unnecessary to use ADO to import the file. Did you try what bhofsetz suggested by recording a macro of you going File>Open in Excel and opening the file up and applying formatting as required?

  6. #6
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Welcome to the forum!

    I don't understand why you would want to append to a file that you query. It would be better to just post 1 ado code example.

    If you can attach a short example csv file, it would be easier to help you.

    As stated earlier, if you record a macro, you will have what you need for a QueryTable. e.g.
    Data > Import External Data > Import Data... > Select your csv file, and finish the wizard. For my csv file with 2 fields, First and Last:
    Please Login or Register  to view this content.
    The array part is where it set the data types.

    We can do the ado if you like but a QueryTable will probably meet your needs.

  7. #7
    Registered User
    Join Date
    01-06-2009
    Location
    Brazil
    MS-Off Ver
    Excel 2003
    Posts
    4
    Perfect, it worked!!! Thanks!

    Just one more question: How can I save it again on a CSV File? That's because I need to make some changes on it and save it back on a CSV format delimited by commas.

    Thanks to all of you!!!

  8. #8
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Here again, the recorder is your friend. Save it as a csv file.

  9. #9
    Forum Contributor
    Join Date
    01-21-2005
    Location
    Colorado
    MS-Off Ver
    2000,2003,2007
    Posts
    481
    Recorded while saving the active file to a csv

    Please Login or Register  to view this content.
    HTH

  10. #10
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    douglasvrs,

    I wrapped you're code this time but please read the below before posting again

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1