+ Reply to Thread
Results 1 to 4 of 4

Open File for Output to xlsx

  1. #1
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Open File for Output to xlsx

    I got my first consulting opportunity for my excel VBA skills for a professional services firm. I've been trying to learn as much as I can so doors open, and lo and behold.

    I realize I have to keep leveling up my game and more doors will open and I will have less grounds for imposer syndrome. I want to have more theoretical and practical knowledge and this question covers both.

    I have the following snippet of code:
    Please Login or Register  to view this content.
    It basically makes a csv file of two columns, a key:value pair. It works, as a csv or txt. I never tried saving it in any other form until now, as an xlsx. When I change the path to .xlsx it saves it alright, but I get this error when I open it:

    "Excel cannot open the file "My File" because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the form of the file."
    It opens as expected when I rename the file to .csv, because that's what it inherently is.

    So I guess my questions are as follows:
    1. Does the open statement only write and read from standard text file formats (because csv is basically a text file with a different designation)? Are there any other formats it can write to?
    2. Is there a way to use the open statement or other function to write to xlsx? The big reason is I want to create a workbook with multiple worksheets for convenience instead of multiple csv workbooks.
    3. I'm researching the modes- Output vs Random vs Binary. Is there any time you would use binary or random? Also I've never specified the access but it always seems I can write. Is that because write is the default? The docs are not very clear on this point : Open Statement docs

    Get better and better, learn what's important and become worth your salt.

    Thanks!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Open File for Output to xlsx

    Hello Foxtrotter,

    Description of the Open As Statement
    1. The Open statement creates a buffer for the file.
    2. The For keyword defines the mode of operation Input, Output, Binary or Random.
    3. Access is Optional keyword and used to specify the operations: Read, Write, Read Write. If Access is omitted you must still specify Read, Write, or Read Write.
    4. The Lock Keyword is Optional. It specifies the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
    5. The Keyword As is required.
    6. # (required) the file number assigned to the open file. The limits are from 1 to 511. Each Open file must have a unique file number. In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number.
    7. Len= specifies the record length (1 to 32,767). This clause is ignored if the file was opened in Binary mode.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: Open File for Output to xlsx

    Thanks for this reply.

    Is there a way I can write to an excel file in a way that supports multiple tabs?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Open File for Output to xlsx

    Hello foxtrotter,

    I don't understand what you mean by "multiple tabs". Do you mean workbooks, worksheets, or something else?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 12-23-2022, 04:03 AM
  2. Replies: 5
    Last Post: 12-23-2022, 04:02 AM
  3. Macro to open a list of spreadsheet and save them under file extension xlsx file
    By gdclyde in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-17-2017, 07:16 PM
  4. Replies: 1
    Last Post: 12-20-2014, 03:14 AM
  5. Replies: 4
    Last Post: 10-23-2013, 12:54 AM
  6. VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2011, 12:12 AM
  7. xlsx file, how to open
    By paresh in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-17-2008, 04:49 AM

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