+ Reply to Thread
Results 1 to 7 of 7

VBA If do not select, then use what? Help to clean up code

  1. #1
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    VBA If do not select, then use what? Help to clean up code

    Please excuse my ignorance, but on this forum, I am generally told NOT to use select or selection because you are able to work with those cells without selecting them.

    So my question is, how do you clean something this simply up without using selection?

    Please Login or Register  to view this content.
    How do you use mutiple commands such as merge and alignment, and/ or font style or type on ONE command line?
    Last edited by clemsoncooz; 01-11-2012 at 02:22 PM.

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA If do not select, then use what? Help to clean up code

    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Re: VBA If do not select, then use what? Help to clean up code

    Quote Originally Posted by tigeravatar View Post
    Please Login or Register  to view this content.
    Hi tigeravatar. Thanks for the response.

    OK - I understand that.

    Is there also another way to do it all on one command line or under one command? I have seen use of an underscore at the end of commands as a continuation, but just wondering how to use it correctly.

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA If do not select, then use what? Help to clean up code

    clemsoncooz,

    The only thing you'd get grief over is using the .Select and Selection statements. Having multiple lines is fine because it makes what the code readable and clearly shows what the code is trying to do.

    This is not recommened, but if you want it all on one line, you would use a colon (":") to tell the code compiler to treat the next statement on the same line as if it were on a new line, like so:
    Please Login or Register  to view this content.


    However, that is much less readable and its harder to see what the code is trying to do.

  5. #5
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Re: VBA If do not select, then use what? Help to clean up code

    OK gotcha. Thank you very much for your help and suggestions. That does help.

    Is there any way to get ride of the with statment?

    Something close to:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA If do not select, then use what? Help to clean up code

    No, for the example provided, you would need the with statement because you're enacting different statements on the same object.
    The comma/underscore/new line is when you're putting multiple arguments for the same statement. Here's an example using the MsgBox statement which has multiple arguments:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    12-20-2011
    Location
    United States, Ohio
    MS-Off Ver
    Excel 2010
    Posts
    295

    Re: VBA If do not select, then use what? Help to clean up code

    Very helpful! Thank you!

    Quote Originally Posted by tigeravatar View Post
    no, for the example provided, you would need the with statement because you're enacting different statements on the same object.
    The comma/underscore/new line is when you're putting multiple arguments for the same statement. Here's an example using the msgbox statement which has multiple arguments:
    Please Login or Register  to view this content.

+ 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