+ Reply to Thread
Results 1 to 10 of 10

IF and Else IF , Is there a better way to code this ?

  1. #1
    Forum Contributor
    Join Date
    01-06-2019
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    209

    IF and Else IF , Is there a better way to code this ?

    I have some code that works fine , but I have to copy and paste the code many times. this can then get confusing when you are changing certain parts of the code.

    I was wondering if it could be put in to functions and then you just call the function u want ?

    Please Login or Register  to view this content.
    Last edited by UltimateNeo; 02-17-2019 at 09:58 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,724

    Re: Is there a better way?

    Before we can continue this thread, please revise your title to give some hint as to your actual question. We ask this for two reasons:

    1. Members scan the list of threads to decide where they can help. The title helps us decide which questions we have the knowledge to solve.
    2. After a question is answered, the title helps people with similar questions find it in a search.

  3. #3
    Forum Contributor
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    365
    Posts
    199

    Re: IF and Else IF , Is there a better way to code this ?

    You should attach a copy of your workbook. It is hard to optimise your code without seeing the data and testing the code to see what it currently does.

  4. #4
    Forum Contributor
    Join Date
    01-06-2019
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    209

    Re: IF and Else IF , Is there a better way to code this ?

    Sorry i haven't been on with the file yet but when i get a chance i will upload, if that helps.

    So what is happening is i have data in the named range "All" and i want to randomly select that 6, 7 or 8 times (Using the function) and put that data as a row using 'i" as the row and then the column is defined by a number. I am also trying to make it select 3 odd then 3 even this will change depending on the if loop which has a multi page select and a combo box that must equal to run said code. As there is a lot of If statements to run basically the same code although the "All" and how many odd and even numbers that are selected would change.

    In the named range "All" the data is from 1 - 59, so instead of repeating the code could i have functions and then just call the function i want in the if statement?

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: IF and Else IF , Is there a better way to code this ?

    What are you using the ArrayList for?
    If posting code please use code tags, see here.

  6. #6
    Forum Contributor
    Join Date
    01-06-2019
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    209

    Re: IF and Else IF , Is there a better way to code this ?

    The Arraylist is storing the cells values, that have been selected and once the row has been completed it will use that array to put the selected cells into the destination.

    From the data in the named range "All", when the code picks a number it stores it in the Array until it has 6, 7 or 8 values, then it can put them into the right place using

    Please Login or Register  to view this content.
    So in this case the Array has 3 numbers that will go into whatever row i equals and column 41.

    The reason it is 3 values instead of 6, 7 or 8 is because i am splitting the array into odd and even numbers, wasn't sure if i could split the array into 3 odd 3 even, or 4 odd 2 even, as a whole array of 6 values.

    As i wanted to be able to choose the amount of odd and even numbers in the final result.

    Hope this helps explain it a little.

    Thank you for your questions

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: IF and Else IF , Is there a better way to code this ?

    Why no use a 'normal' array for that?

  8. #8
    Forum Contributor
    Join Date
    01-06-2019
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    209
    Quote Originally Posted by Norie View Post
    Why no use a 'normal' array for that?
    Because i was wanting to decide on how many were odd and how many were even. Wasn't sure how to do it in 1, so split it into 2. To deal with it seperatly

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: IF and Else IF , Is there a better way to code this ?

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.

  10. #10
    Forum Contributor
    Join Date
    01-06-2019
    Location
    Australia
    MS-Off Ver
    2019
    Posts
    209

    Re: IF and Else IF , Is there a better way to code this ?

    I have uploaded a sample file. It just doesn't have the userform. the userform will be the if statement as it has a multi page tab as 1 of the parts to the if statement and then a combo box for the other part both have to equal the values selected.

    As i would not like ot just copy and paste the code for each if, then , elseif of each multi page tab and combo box configuration.

    There are 6 multipages and the comboxbox would have 6 configurations (All,Half,Other Half,) for example

    Hope this helps

    It is basically i want this code in a multiple functions called by a sub that can call the correct function, depending on the multi page tab and combo box. With the functions i would like to change the amount of odd to even numbers picked ratio. So there would be multiple functions for example
    (3 ODD 3 Even), or (4 Odd 2 Even). If i can have 1 array of 6 or 7 and do it in 1 array that would be better. I couldn't see how that part could be done so that is why i split it int o2 parts 1 for Odd and 1 to deal with Even.

    Please Login or Register  to view this content.
    Thank you for your help
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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