+ Reply to Thread
Results 1 to 3 of 3

Retain the Value of a Variable in Macro

  1. #1
    Registered User
    Join Date
    07-18-2007
    Posts
    16

    Retain the Value of a Variable in Macro

    Hi,

    How could I retain the value of a variable that I used in a macro so that next time this macro is run, the value for this variable is still remembered? The case is, I need to record the line numbers that I have read from a text file in a macro. The next time I run the macro, I need to get the value and start from that line to the bottom of that file. E.g. This time the lines imported are 240, the next time I need to read the file from line 240 to the bottom (E.g. 300). Next time I need to read the file from line 300.....

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Declare the variable static, as in this
    Please Login or Register  to view this content.
    will keep the value of myVariable from one running of the macro to the next.
    Static variables will reset when the workbook is closed. If you want a value to persist between close and open, you could write it to a cell, a Name or a custom Property.

  3. #3
    Registered User
    Join Date
    07-18-2007
    Posts
    16
    Thanks!! I need to retain the value even though the workbook is closed. The suggestion to write it to a cell is a good idea! Why I haven't thought about that!

+ 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