Multiple If-Then statements - a better way?

TheShaunMichael

Board Regular
Joined
Oct 24, 2009
Messages
57
Hello,

I have 9 separate macros setup, each designed to create a document in MS Word. What happens now, is I have a userform open with 9 buttons and I press the button for the type of document I need in MS Word.

What I'd like to do is create a series of If-Then statements to have Excel just automatically choose the correct Macro/Document. There are several variables in my Excel spreadsheet which dictate the document I need and I am thinking there is an easier way to write this code than by creating essentially several layers of If-Then statements (maybe select case???). I'm thinking about testing for the presence of each of these variables separately and assigning it a number. Then based on the total sum I can assign the MS Word template accordingly.

Here is the information i'm working with:

Dim EI As Worksheet
Set EI = Worksheets("Estimate Info")
Z = ActiveCell.Row

NewTotal = EI.Cells(Z, 13) - testing for a value greater than 0
FormTotal = EI.Cells(Z, 16) - testing for a value greater than 0
DTPTotal = EI.Cells(Z, 25) - testing for a value greater than 0
FuzzyTotal = EI.Cells(Z, 28) - testing for a value greater than 0
GoldTotal = EI.Cells(Z, 31) - testing for a value greater than 0
SourceLang = EI.Cells(Z, 18) - testing for the word "English"
TargetLang = EI.Cells(Z, 19) - testing for the word "English"
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
One way:

Code:
    Dim myCase      As Long
 
    With Worksheets("Estimate Info").Rows(ActiveCell.Row)
        myCase = WorksheetFunction.SumProduct(Array(-(.Cells(13) > 0), _
                                                    -(.Cells(16) > 0), _
                                                    -(.Cells(18) = "English"), _
                                                    -(.Cells(19) = "English"), _
                                                    -(.Cells(25) > 0), _
                                                    -(.Cells(28) > 0), _
                                                    -(.Cells(31) > 0)), _
                                                    Array(64, 32, 16, 8, 4, 2, 1))
    End With

    Select Case myCase
        Case 127    ' all true
            '...
        Case 0  ' none true
    End Select
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top