+ Reply to Thread
Results 1 to 10 of 10

VBA code Excel to .DAT file

  1. #1
    Registered User
    Join Date
    09-16-2014
    Location
    London
    MS-Off Ver
    2010 (In 2024)
    Posts
    24

    VBA code Excel to .DAT file

    Hi,

    I am fairly new to VBA and I am looking for a code that will select a cell range and export it to a .DAT file to be saved in a particular folder.

    The cell range is column E1:E776 on a sheet named "SNAP Output"
    I would like the routine to select these cells on that worksheet and export them as a .DAT file to be saved in a file path named C:\SNAP
    The name of the file needs to be taken from cell E7 on a sheet named "SNAP-1" (this is also the sheet where I will be running the command from).

    Hopefully someone can help me with this!

    Many Thanks.

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code Excel to .DAT file

    'DAT' is an arbitrary extension and is not a defined file format like 'CSV' or 'XLS'

    You can save an Excel workbook with a DAT extension and Excel will still open it, it'll complain that the extension and file format do not match but will open if you confirm.

    In short, you need to explain the format of this DAT file - it may well be a CSV format, or Tab delimited, internally but you need to confirm that. If you don't know the format then upload a small example of an existing file.

  3. #3
    Registered User
    Join Date
    09-16-2014
    Location
    London
    MS-Off Ver
    2010 (In 2024)
    Posts
    24

    Re: VBA code Excel to .DAT file

    I don't need Excel to open the file once it has been exported (it will then we opened in a pipe sizing programme that can read .DAT files). I can upload an example of the .DAT file tomorrow if it is still required.

    Thanks

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code Excel to .DAT file

    My comment about Excel was just to illustrate the extension really means nothing. The internal format is what defines if a program can read the file

    As you still haven't described the file format, and 'Pipe Sizing Program' really means nothing so, yes, a sample of the file is needed.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: VBA code Excel to .DAT file

    I agree with cytop's comments. Typically, in my experience, a .DAT file is often simply a Text file with a different (.DAT) extension.

    So, a simplistic process would be:
    1. create a new empty temporary sheet
    2. copy the required cells to the new sheet starting at cell A1.
    3. save the new sheet as a .txt file, or a .csv file, or whatever.
    4. as part of the save process (in 3) name the file and give it a .DAT extension
    5. delete the temporary worksheet

    It kind of depends on what the pipe sizing program is expecting so, as well as providing a typical .DAT file, you should also provide a sample of the Excel workbook that will be used as the basis for the .DAT file.

    Sometime this sort of stuff is messier than it sounds.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  6. #6
    Registered User
    Join Date
    09-16-2014
    Location
    London
    MS-Off Ver
    2010 (In 2024)
    Posts
    24

    Re: VBA code Excel to .DAT file

    Thanks for your replies.

    I have tried uploading the file but it wouldn't let me as it says it is an invalid file type. An example of the code I have come up with so far is:

    Sub test()

    Open ThisWorkbook.Path & "\File1.dat" For Append As #1
    Print #1, "SNAP Output"; .Range("E1").Value
    Close #1

    End Sub

    This copies the data in cell E1 and saves it as the file type I am looking for in the same folder as the spreadsheet.
    The parts I am struggling with are:
    1. Changing where it is saved too (I want the file to go to a different directry to where the spreadsheet is located)
    2. The cell range (I can only get it to work for one cell - I assumed this would just need to be .Range ("E1:E750") - but it fails when I do that.
    3. I want the .DAT file to be named by the value in a cell each time (not the same each time as I currently have it).

  7. #7
    Registered User
    Join Date
    09-16-2014
    Location
    London
    MS-Off Ver
    2010 (In 2024)
    Posts
    24

    Re: VBA code Excel to .DAT file

    I believe TMS is correct when saying that it is a Text file with a .DAT extension.

  8. #8
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code Excel to .DAT file

    OK - the forum won't allow a DAT file to be uploaded, but just to prove a point about the extension you could rename the file like XXX.DAT.XLS (the multiple extensions don't matter - it's the last one that will be used) - you'd also need to mention the renaming in the message.

    Might suggest you manually copy the range E1:E750 to a new workbook and then save that as a CSV or TXT file. Excel will force the extension to CSV or TXT so you need to rename the file to .DAT after.

    That is essentially what the code you posted does, so if your uploader handles that file correctly the code you posted can be edited as you know the internal format of the file is as expected.

    Sorry, not being pedantic but I prefer to work with known facts rather than what someone believes. Things usually work out quicker that way.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: VBA code Excel to .DAT file

    Again, agree with cytop. Either change the extension to .txt, add an additional .txt extension, or zip the file up and upload that. Don't think you need to copy to a workbook, you should just be able to copy to a sheet. And I would have thought you could call the file whatever you want.

    Let's get some examples up to play with

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,494

    Re: VBA code Excel to .DAT file

    Re point 1; don't use ThisWorkbook.Path. Specify the complete directory, folder, sub-folder structure. That bit should be easy.

    Re point 2; do you want a separate line/entry for each cell? If that's the case, you just need a loop.

    Re point 3; not a problem

+ 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. vba code for opening excel file and insert a new column in exisiting excel file
    By saratu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-21-2014, 02:55 PM
  2. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  3. [SOLVED] Code to open another excel file, file name depending on cell contents
    By L-Drr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2013, 11:09 AM
  4. Converting excel 2003 file ext macro file-save code to reflect excel 2007 exts
    By pmanoloff in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2013, 12:17 PM
  5. Excel 2010 file with VBA save and send code works-but file can't be found.
    By carlton.clay in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-13-2013, 09:46 AM
  6. [SOLVED] code to check file size everytime an Excel file is opened
    By Kaiser in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-30-2006, 12:50 PM
  7. Import VBA Code in Excel-File ? (Export VBA Code to file)
    By Matthias Pospiech in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-22-2005, 01:06 PM

Tags for this Thread

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