Single or Multiple Variables to Populate Message Boxes in Different Subs
Results 1 to 2 of 2

Thread: Single or Multiple Variables to Populate Message Boxes in Different Subs

  1. #1
    New Member
    Join Date
    May 2019
    Post Thanks / Like
    0 Post(s)
    0 Thread(s)

    Default Single or Multiple Variables to Populate Message Boxes in Different Subs

    I normally use a string variable (Var_MsgText) as an input to message boxes but I'm currently writing my biggest piece of code to date that has multiple subs that all use message boxes. I don't want Var_MsgText to be a public sub as that would risk incorrect carry over of the content between different subs so the two approaches I'm considering are:

    Approach 1: Var_MsgText1 in one sub, Var_MsgText2 in the next sub etc, that this means creating more variables but maybe I'm worrying about nothing

    Approach 2: Have a single variable name (Var_MsgText) that's used in multiple subs but re-declaring it at the beginning of each sub so that its empty, is that possible or will it cause problems?

    Approach 3: Someone gives me a better idea than 1 or 2

    Thanks in advance.

  2. #2
    Board Regular sykes's Avatar
    Join Date
    May 2002
    Post Thanks / Like
    4 Post(s)
    0 Thread(s)

    Default Re: Single or Multiple Variables to Populate Message Boxes in Different Subs

    I'm not an expert, by any means - and stand to be corrected (or shot down in flames) by some very clever Folk who help on this great site.
    That having been said, for my two pennies worth:

    1. Personally, as long as you're not needing the variable (presumably a string?) to service several of your msg boxes at the same time, I'd declare it publicly at the top of a module - with the workbook_open event. It's done then - once and for all, and after all, it's only an instruction to the computer, to hold a tiny bit of memory free for your code to use, so there's no need to re-declare it, or clear it out each time - just use it, as you would any variable; each time you refer to it, the previous data's cleared out, and the new string inserted. No need to declare it any more in any of your code, so it keeps things slightly more curt, too.

    2. One other thing - personally, I've stopped using message boxes unless absolutely necessary. I find that it quickly gets tedious for you (or even more so for your users) to have message boxes popping up all the time, and having to react to them. I tend to use UserForms more now days - often small ones which are permanently displayed (by setting the "modeless" property) and having label controls on them. It's just as easy to then use the "Caption" property to pass messages to your users, and whilst it's not quite as "attention-getting" I find it a tidier and more user-friendly way to communicate.

    Windows 10 / XL 2016
    Members don't have crystal balls, so PLEASE describe your situation, code,
    and requirements ACCURATELY in your first post. It saves hair!
    Better still - upload your spreadsheet (and formulae!) with this great bit of kit.

    - forum use guidelines, forum rules and terms of use
    - Try searching for your answer first, see how
    - Read the FAQs
    - List of BB codes

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts