5

I have an Excel sheet with a script and it's saved on a remote location that every user can access. This macro will let user select a target folder and create some files inside.

Now I want my script to remember the last chosen path of each user. I might need to store more paths (variables) in the future.

My idea is to store those data in a Environ("AppData") location on each users' computer. Sort of an offline cookie.

But what would be the easiest way to create (if it doesn't exist) read from and update file with multiple that I want to easily access?

I've tried it saving in notepad, but indexing and parsing was nightmare.

4
  • Create a hidden sheet where you can add entries for each user, you can retrieve user names with Environ("username")
    – Raugmor
    Oct 6, 2015 at 12:55
  • 1
    SaveSetting/GetSetting is the easiest way to store named values (in the registry)
    – Alex K.
    Oct 6, 2015 at 12:57
  • 1
    When it's on the remote location, it's set to read only. Even so, the spreadsheet would need to be shared (which is not user-proof) and it would ask to save changes after each use.
    – Zikato
    Oct 6, 2015 at 12:58
  • What about using an Access DB intead of notepad? I think it is easier to manage, cleanup etc than using registry, and much easier to parse and query than notepad.
    – A.S.H
    Oct 6, 2015 at 13:06

1 Answer 1

14

Save the settings to the registry.

SaveSetting (appname, section ,key ,setting)

SaveSetting "AffectedTerminals", "frmMain", "LastDir", szPathname

Then you can retrieve the setting with getsetting the next time your app runs so you can use it.

GetSetting ( appname , section, key [, default ] )

Dim szLastDir As String
szLastDir = GetSetting("AffectedTerminals", "frmMain", "LastDir", "P:\AttEngineering")

EDIT: appname and section arguments explained.
The appname and section can be anything you want. Something that describes the setting is best. In the above code AffectedTerminals was the name of my application. So if you have a spreadsheet that handles IT invoices then you might call the appname "ITinvoices". The section is just a sub section (sort of a sub-directory) for the regestry entry.

The setting will be saved at this locatioin

HKEY_CURRENT_USER/Software/VB and VBA Program Settings/appname/Section/key/value/

So for mine it is saved as enter image description here

SaveSetting Function

AppName
Required. String expression containing the name of the application or project to which the setting applies.

Section
Required. String expression containing the name of the section in which the key setting is being saved.

Key
Required. String expression containing the name of the key setting being saved.

Setting
Required. Expression containing the value to which Key is being set.

GetSetting Function

appname
Required. string expression containing the name of the application or project whose key setting is requested.

section
Required. String expression containing the name of the section where the key setting is found.

key
Required. String expression containing the name of the key setting to return.

default
Optional. Expression containing the value to return if no value is set in the key setting. If omitted, default is assumed to be a zero-length string ("").

4
  • This looks good, I just have a question about parameters "appname" and "section". Because my script is saved in excel, would the appname param be the full path to excel file? And sectioname can be anything or is there a logic to it? Thanks!
    – Zikato
    Oct 6, 2015 at 13:16
  • 1
    No, it can be anything you want. Something that describes the setting is best. In the above code AffectedTerminals was the name of my application. So if you have a spreadsheet that handles IT invoices then you might call the app "ITinvoices". The section is just a sub section for the regestry entry. I'll explain in my answer in just a minute.
    – MatthewD
    Oct 6, 2015 at 13:21
  • 1
    Great answer, thanks! I'll wait couple of days if there are other options before accepting.
    – Zikato
    Oct 6, 2015 at 13:36
  • Glad to help. Let me know if you have any further questions on this.
    – MatthewD
    Oct 6, 2015 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.