+ Reply to Thread
Results 1 to 8 of 8

Shorten IF/ElseIF statement when making Items visible/not visible

  1. #1
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sefton
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    133

    Shorten IF/ElseIF statement when making Items visible/not visible

    Is there a way to shorten a code similar to the below? (i know it's not a complete code - it's an example) It just keeps going until they're all TRUE and then ends with an Else which has them all set to FALSE.

    ValueX is determined elsewhere
    Values A,B,C etc will be in increments of 1

    Please Login or Register  to view this content.
    I'm new to code (learning as I go) so if you could explain the different parts of your code it would be very helpful.
    Last edited by DHFE; 11-05-2015 at 08:24 AM.

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    Hi DHFE,

    The only thing I would suggest with the existing code, is a different method of indenting. There may be ways to shorten the code, but as written, the code is very easy to read and to maintain.

    I would write the code as:
    Please Login or Register  to view this content.
    Lewis

  3. #3
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sefton
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    133

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    As i stated, this is an example - it's not what the final code looks like. I'm looking for something that will take ValueX compare it against Values A,B,C etc and set the Frames.Visible to True or False accordingly without me having to write 80+ lines of code just to get what i want - especially since i have to enter similar versions of this code into 5 or 6 separate UserForms.
    Last edited by DHFE; 11-05-2015 at 09:07 AM.

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    Hi,

    Please upload the workbook so that we can see in context.
    How many Frames are there? Your brief explanation suggests that there are as many Frames as there are Values A,B,C etc. since that's implied by your statement that "It just keeps going until they're all TRUE", although there is some doubt since you then say "and then ends with an Else which has them all set to FALSE." which might suggest there are fewer Frames than there are A.B,C etc..
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  5. #5
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sefton
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    133

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    At the moment there's 10 frames and values 'A,B,C' are 1, 2, 3, 4 & 5 and two frames change from False to True with each increment of the value

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    The following was done in an ordinary code module. Remove the "UserForm1." if done in the UserForm module:
    Please Login or Register  to view this content.

  7. #7
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sefton
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    133

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    as i stated in my original post, i'm new to code so could you explain what's happening and where? At the moment I'm getting a type mismatch error

    edit:

    Never mind i got it to work using the following;
    Please Login or Register  to view this content.
    Last edited by DHFE; 11-11-2015 at 07:02 AM.

  8. #8
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Shorten IF/ElseIF statement when making Items visible/not visible

    Hi,

    Thank you very much for the rep points. I'm glad you got it working.

    For future reference:

    'Option Explicit' is a MUST.
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Debugger Secrets:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

+ 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. Select visible range and add page break after every 20th visible cell
    By tuc47285 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-04-2015, 01:18 AM
  2. [SOLVED] Making checkboxes not visible
    By Elizabethd1012 in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 05-01-2014, 02:27 PM
  3. Replies: 4
    Last Post: 01-06-2014, 02:14 AM
  4. Table Object after filtering - Count visible rows & First visible row
    By limalf in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-13-2013, 07:29 PM
  5. Making Formulas Visible
    By thorgal67 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 12-15-2010, 11:40 AM
  6. Visible frames based on combobox
    By rajeev.raj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-29-2010, 07:57 AM
  7. Resize Visible Rows based only on Visible Columns text
    By Zimbo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-22-2009, 10:55 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