+ Reply to Thread
Results 1 to 10 of 10

Maximum Number of DIM and Set Usage

  1. #1
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Maximum Number of DIM and Set Usage

    Greetings,
    I am writing a Macro that may end up with 30-40 "DIM" and "Set". Is there a maximum number that can be used, or is there a shortcut to reduce the number. Ex:
    Please Login or Register  to view this content.

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Maximum Number of DIM and Set Usage

    The only limit is memory. It actually helps as Variant type takes up the most memory. I like it because it keeps your variable's case.

    I usually dim r as Range and reuse it. If you have many ranges, you could put string addresses into an array and reuse when needed.

  3. #3
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Maximum Number of DIM and Set Usage

    Kenneth,
    I'll work with your suggestion and see what I come up with.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Maximum Number of DIM and Set Usage

    Hi there,

    For what it's worth, I'd say that a single routine which incorporates 30-40 variables is WAY too large, and is a prime candidate for splitting into several smaller routines.

    Regards,

    Greg M

  5. #5
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Maximum Number of DIM and Set Usage

    Greg,
    Can you give me direction as to how I can accomplish this.

  6. #6
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Maximum Number of DIM and Set Usage

    Hi again,

    It's not really possible for me to make suggestions without sight of your code, but almost certainly your single 30-40 variable routine is performing several tasks (e.g. reading data from somewhere, processing that data, and then sending the processed data to somewhere else), If you look closely at how your routine works you can probably break it down into a small top-level routine which calls a data-reading routine and retrieves the data (e.g. in an array or a range), passes that array/range to a data-processing routine and retrieves an array/range of processed data. The top-level routine then passes that array/range of processed data to a data-output routine.

    The advantage of this approach to coding is that it is much easier to test/debug/redesign individual small single-purpose routines than a large routine which incorporates several different tasks. When writing routines, a principle I try to adhere rigidly to is that big is definitely NOT beautiful!

    Sorry I can't be more specific, but please feel free to ask about anything you think I might be able to help with.

    Regards,

    Greg M

  7. #7
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Maximum Number of DIM and Set Usage

    Greg,
    Thank you for your encouragement to research this problem! I came across the following which will reduce the number of Dim Statements. Also, can the number of Set variables be reduced as well?
    Please Login or Register  to view this content.

  8. #8
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Maximum Number of DIM and Set Usage

    Hi again,

    I think you may be mixing two separate issues here.


    Issue one - the one raised in your original post:

    Using an array (Tracker) instead of four separate variables reduces the number of Dim statements required, but doesn't really change anything - i.e. the computer still has to work with four variables, BUT, it makes life more complicated for YOU, because when writing your code you now have to deal with the non-intuitive indices 1-4 instead of conveniently-named variables (mt_Re_Obligation etc.)

    You can't really change the number of Set statements - if you need to assign a value to a Range object, you need an associated Set statement, in just the same way that you need to use an equals sign if you want to assign a numeric value to a variable (iRowNo = 5). Yes, you can of course assign the values from an entire Range to an array with a single equals sign (vaDataValues = ActiveSheet.Range("A1:E10").Value), but that's quite a separate issue.


    Issue two - the one I raised in my original post:

    The point is not that there are too many Dim/Set statements in your single routine, but that there are too many variables in your single routine. This is the point I was making and the point I tried to describe (admittedly in very general terms) in my subsequent post.


    Hope this helps, but as before, feel free to ask any further questions you might have.

    Regards,

    Greg M
    Last edited by Greg M; 09-29-2019 at 10:32 AM. Reason: Typo corrected

  9. #9
    Forum Contributor
    Join Date
    07-07-2017
    Location
    USA
    MS-Off Ver
    2010
    Posts
    500

    Re: Maximum Number of DIM and Set Usage

    Greg,
    Again thank you for your insight and patience. I'll continue to work on it and see what I come up with.

  10. #10
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Maximum Number of DIM and Set Usage

    Hi again,

    You're welcome. You know where to find me if you have further questions.

    Regards,

    Greg M

+ 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. [SOLVED] How to color maximum number by Green and minimum number by Red..
    By mohit999 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 03-02-2016, 02:26 PM
  2. [SOLVED] Maximum Number
    By geliedee in forum Excel General
    Replies: 8
    Last Post: 01-06-2015, 08:35 AM
  3. variable monthly usage with maximum limit.
    By excobra in forum Excel General
    Replies: 1
    Last Post: 05-29-2014, 06:41 AM
  4. Workbook usage or Database usage assistance
    By Sunshine601 in forum Excel General
    Replies: 4
    Last Post: 12-12-2013, 08:36 AM
  5. Replies: 11
    Last Post: 11-26-2012, 08:46 PM
  6. if then to a MAXIMUM number
    By atlus in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-05-2009, 09:26 AM
  7. [SOLVED] getting the number of the row with the maximum value
    By hilbert in forum Excel General
    Replies: 3
    Last Post: 04-05-2005, 09:06 AM

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