+ Reply to Thread
Results 1 to 9 of 9

Excel - Best Practices Question - Usage of 'With' statement

  1. #1
    Registered User
    Join Date
    01-18-2013
    Location
    Michigan, USA
    MS-Off Ver
    MS Office 2016
    Posts
    12

    Excel - Best Practices Question - Usage of 'With' statement

    I have received conflicting advice re: usage of the 'With' statement.

    A review of my coding by an experienced coder was critical w/ regard to usage like this (fabricated example below):

    Please Login or Register  to view this content.
    The issue: only one object following the 'With' statement - proper form would be 3 distinct lines of code, each starting with 'With wksSample' like this:

    Please Login or Register  to view this content.
    I was told a common 'dot operator' was required to follow the initial object (example below). If that common operator does not occur, 'With' should not be employed.

    Please Login or Register  to view this content.
    1) Opinions re: this commonly-occurring coding scenario?

    2) Q: It was my belief, when a number of 'things' are done to an object, that employing the 'With' operator speeds up the code. True/False?

    3) Moreover, what about UserForm coding? Example: I presently have many sets of lines of code like this:

    Please Login or Register  to view this content.
    Can... rather, should this be done?

    Please Login or Register  to view this content.
    My thanks in advance.

    RC (ralphjmedia)
    Last edited by ralphjmedia; 09-09-2014 at 11:46 AM.

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,447

    Re: Excel - Best Practices Question - Usage of 'With' statement

    Hi,

    According to Mr Pearson, "with" is a tactic that will speed up execution of code

    http://www.cpearson.com/excel/optimize.htm
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    01-18-2013
    Location
    Michigan, USA
    MS-Off Ver
    MS Office 2016
    Posts
    12

    Re: Excel - Best Practices Question - Usage of 'With' statement

    Sorry! Will do so in future. Post formatting corrected (I believe). Is it now eligible to receive/display additional responses? Thanks.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Excel - Best Practices Question - Usage of 'With' statement

    Aside from performance improvement, the With statement eliminates duplication; you only need to change the object name in the With statement.
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Excel - Best Practices Question - Usage of 'With' statement

    It depends on the context, the main argument against it is simply that it is not as readable - this seems to be why it wasn't added to C# where it still exists in VB.Net. Of course whether this is actually the case is highly subjective - in this context it is pretty unambiguous, however since it is possible to have nested "With"s and there is no auto indentation in the IDE, things can become a bit hairy if you use it a lot and don't indent properly.

    I'm also somewhat sceptical that it actually offers any noticeable performance improvement over declaring a variable and referencing that.

    In the end, it's personal preference - it's really up to you whether you use i or not, there's no right answer.

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Excel - Best Practices Question - Usage of 'With' statement

    It depends on the context, the main argument against it is simply that it is not as readable - this seems to be why it wasn't added to C# where it still exists in VB.Net. Of course whether this is actually the case is highly subjective - in this context it is pretty unambiguous, however since it is possible to have nested "With"s and there is no auto indentation in the IDE, things can become a bit hairy if you use it a lot and don't indent properly.

    I'm also somewhat sceptical that it actually offers any noticeable performance improvement over declaring a variable and referencing that.

    In the end, it's personal preference - it's really up to you whether you use i or not, there's no right answer.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,339

    Re: Excel - Best Practices Question - Usage of 'With' statement

    I'd ask your experienced coder why he thinks it's better to write the same variable three times.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Registered User
    Join Date
    01-18-2013
    Location
    Michigan, USA
    MS-Off Ver
    MS Office 2016
    Posts
    12

    Re: Excel - Best Practices Question - Usage of 'With' statement

    OK, to better clarify, here's the simple either/or:
    Setting aside issues of personal preference, readability, or microseconds of processing time possibly saved/lost - is this 'legal' or bad practice (below)?

    Please Login or Register  to view this content.
    I was told that a "dot operator" was required after the object when electing to employ the 'With' statement - just the object, followed by nothing, and then a collection of operations below, closed by an 'End With' was wrong - if no common operators exist, the above code should look like this:

    Please Login or Register  to view this content.
    It appears to work either way, and of course, there's no discernable execution time when it comes to a single block of code.
    Moving forward, I am looking for the best method to employ across a complex application that contains a number of UserForms, Charts, Ranges, etc. that all see a number of operations applied, depending on conditions that change based on user input (clicking a command button, selecting a ComboBox choice, etc).

    Again, my thanks.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Excel - Best Practices Question - Usage of 'With' statement

    Both are fine and purely down to personal preference. Makes absolutely no difference either way

+ 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] Activecell.Address statement usage
    By ag273n in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-06-2013, 08:33 PM
  2. If statement usage
    By sekharsomu in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-16-2009, 03:44 AM
  3. CPU usage question for excel 2007 users.
    By feejo in forum Excel General
    Replies: 2
    Last Post: 12-27-2007, 09:49 AM
  4. best practices question
    By Gary Keramidas in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2006, 06:15 AM
  5. Best practices for pulling data from a network Excel file (mult users)
    By KR in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-07-2006, 03:45 PM

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