+ Reply to Thread
Results 1 to 6 of 6

Write data to a file

  1. #1
    Registered User
    Join Date
    04-22-2009
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Write data to a file

    I would like to be able to enter data to a counter in a VB macro so that every time I run the macro it updates the counter. This is useful for changing the background color each time the macro runs to distinguish data. I know it can be done by enter a value in the excel spreadsheet, but I'd like to somehow pass this data to a file that could be accessed by any of my macros. I've searched Excel and VB help about how to write data to files, but can't make sense of their code examples. Please advise. Thanks.

  2. #2
    Registered User
    Join Date
    04-07-2009
    Location
    San Antonio, TX
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Write data to a file

    Writing to a file is a somewhat easy task the fun part is reading from it. That stuff is a mess. If all the macros are in the same workbook you could simply Dim a variable as public/global and it would retain it's value after the code in the module ran. it would also make it accessable to everything in your VBA projects for the workbook. Unfortunetly it would lose it's value once you closed the book.

    Example of a Public Dim statement (it is outside all subs and functions):
    Public Features_On_Select As String

    For Reading a file you can use a function like this. I didn't write it i got it from somewhere months ago.

    Please Login or Register  to view this content.
    example for writing to a file (see same comment as above):
    Please Login or Register  to view this content.
    Here is my function Path() so that you know what it is doing:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-22-2009
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Write data to a file

    thanks for Replying.

  4. #4
    Registered User
    Join Date
    04-22-2009
    Location
    Austin, Texas, USA
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Write data to a file

    thanks for replying, but I could not get these macros to work for me although the function did work. I think I may have found some simple code that will work for the immediate. However in searching Excel VB help they said that the following code should write to a file but I could not get it to work:

    Please Login or Register  to view this content.
    Any Suggestions as to why? Thanks
    Last edited by austindude; 05-04-2009 at 01:17 AM. Reason: Forum Infraction

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Write data to a file

    austindude

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Write data to a file

    The macro code does write to a file called TESTFILE

    It has no extention n the name or path so the file may not be were you are looking for it

    Suggest make file name Testfile.txt
    The default location is probally in your My documents folder

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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