+ Reply to Thread
Results 1 to 3 of 3

Simplify blocks of conditional statements

  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Manila, Philippines
    MS-Off Ver
    2019
    Posts
    102

    Simplify blocks of conditional statements

    Hello Excel champions!

    While the block of code below works however, it looks quiet an eye-sore as there are so many iterations that I believe can be simplified with nested loops. While I am currently fighting my intimidation to Looping statements, can you guys shed a light on me as to how this can be reduced into its simplest form? I will be going to study your solutions and hopefully get past my fear in looping.

    Thanks in advance.

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    01-19-2012
    Location
    Barrington, IL
    MS-Off Ver
    Excel 2007/2010
    Posts
    1,211

    Re: Simplify blocks of conditional statements

    I'm no VBA expert so take this with a dash of salt...but...

    I would make an array variable with inserted values in multiples of two. So 9 textboxes would look like


    if Textbox1 = "" then 2
    if Textbox2 = "" then 4
    if Textbox3= "" then 8
    if Textbox4 = "" then 16
    if Textbox5 = "" then 32
    if Textbox6 = "" then 64
    if Textbox7 = "" then 128
    if Textbox8 = "" then 256
    if Textbox9 = "" then 512


    then sum the array in each if statement. then you know if the sum of the array is 192, the only possible reason is textbox 6 and 7 are blank and the rest are populated, so you can use that instead of the variation of textbox#.value <>/=""


    You can build a legend easily in excel to figure out what the conditions would be:

    Cells A1 through A10 represent Textbox1 through Textbox10
    In B1 use the formula =2^ROW() and copy the formula down
    In C1 through C10, put 1 or 0 to show the condition of blank textbox or not blank textbox (put a 0 when you want to see blank or 1 for not blank for example)
    In D1 put =SUMPRODUCT(A1:A10,B1:B10) this will be your result


    So now you can do something like:

    Please Login or Register  to view this content.
    with something like
    Please Login or Register  to view this content.
    Last edited by Speshul; 09-03-2015 at 10:57 AM.
    You should hit F5, because chances are I've edited this post at least 5 times.
    Example of Array Formulas
    Quote Originally Posted by Jacc View Post
    Sorry, your description makes no sense. I just made some formula that looks interesting cause I had nothing else to do.
    Click the * below on any post that helped you.

  3. #3
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Simplify blocks of conditional statements

    it can be reduced down a bit quite easily however it may be more sensible to look at the 4 Subs you're calling. Are they wildly different to each other or could you not simply pass the required textbox values into there and incorporate the logic into one Sub?

    Some of the changes I can see (or potential flaws in the logic) are:

    All of the IF statements start with checking textbox1 isnt blank but a blank isnt possible as previous IF has taken care of those.

    textboxes 5 and 9 are always required to be blank so you should probably check these in one IF statement and drop out then (probably do these as the 2nd IF statement). You can then remove those two from the other IFs to make them more readable.
    If someone has helped you then please add to their Reputation

+ 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. Conditional formatting to shade blocks of rows
    By MRSH in forum Excel General
    Replies: 2
    Last Post: 07-23-2015, 07:16 AM
  2. [SOLVED] Colour conditional formatting alternating blocks of rows
    By Deanomcbeano in forum Excel General
    Replies: 7
    Last Post: 10-03-2012, 01:48 PM
  3. Using macros to identify blocks of data and summarise those blocks
    By gophbeav in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-05-2012, 04:35 AM
  4. Simplify and Combine Complicated Conditional Formula
    By amyxkatexx in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-13-2010, 02:51 PM
  5. Conditional IF statements
    By Story in forum Excel General
    Replies: 2
    Last Post: 07-24-2009, 12:28 PM
  6. Excel 2007 : Title blocks and x+y blocks!!!!!!
    By ginabgirl in forum Excel General
    Replies: 1
    Last Post: 11-10-2008, 05:47 PM
  7. IF conditional statements
    By Alex Martinez in forum Excel General
    Replies: 2
    Last Post: 08-22-2005, 03:05 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