Multiple IF statements, is there a better way?

Nothnless

Board Regular
Joined
Apr 28, 2016
Messages
142
Hi guys, here is some code but I suppose there would be a better way to do it?

What I'm doing is, on button press check to see if a checkbox is checked on the userform and if so call a routine that looks in column A and hides the row.

For example, if checkbox A is checked then call routine A_hide() the Sub says something like for every A in field A hide the row.

Problem is I'm doing this for each checkbox and I have like 40 of them:

Code:
If ToolBox.CheckBox_IMPpgs.Value = True Then    Call IMPpgs_Filter
    End If
    If ToolBox.CheckBox_OnPGs.Value = True Then
    Call OnPGs_Filter
    End If
    If ToolBox.CheckBox_Simple.Value = True Then
    Call Simple_Filter
    End If
    If ToolBox.CheckBox_Plus.Value = True Then
    Call Plus_Filter
    End If
    If ToolBox.CheckBox_Trad.Value = True Then
    Call Trad_Filter
    End If
    If ToolBox.CheckBox_HD.Value = True Then
    Call HD_Filter
    End If
    If ToolBox.CheckBox_MPO.Value = True Then
    Call MPO_Filter

Is there a better way?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
There is surely a easier way.

You need to show us a couple of the scripts.

Your showing us Call Something

But I can not see what your script is doing

You only said:

something like for every A in field A hide the row.

That is not very specific at all

For every A in field A

Do you mean look in column A for "A"
And on each row with "A" hide that row.

And look in Column B for "B" and Hide that row.

And we would need to loop through the CheckBoxes

CheckBox1 for column(A"
CheckBox2 for column("B")
 
Upvote 0
There is surely a easier way.

You need to show us a couple of the scripts.

Your showing us Call Something

But I can not see what your script is doing

You only said:

something like for every A in field A hide the row.

That is not very specific at all

For every A in field A

Do you mean look in column A for "A"
And on each row with "A" hide that row.

And look in Column B for "B" and Hide that row.

And we would need to loop through the CheckBoxes

CheckBox1 for column(A"
CheckBox2 for column("B")

Yes that is correct except it's all in column A. So look in A and hide rows with A if checkbox A is checked and also hide rows with C if checkbox C is checked and hide rows with E if checkbox E is checked etc...
 
Upvote 0
You just said Checkbox is name A and B
So why does your script here say:
If ToolBox.CheckBox_HD.Value

Looks like this checkbox is named
ToolBox.CheckBox_HD


I asked to see the code you have in your call statement and you did not show me the code.

I want to see this code:

Call MPO_Filter
<strike>
</strike>
 
Upvote 0
You just said Checkbox is name A and B
So why does your script here say:
If ToolBox.CheckBox_HD.Value

Looks like this checkbox is namedToolBox.CheckBox_HD


I asked to see the code you have in your call statement and you did not show me the code.

I want to see this code:

Call MPO_Filter
<strike>
</strike>


Hi, sorry I was trying to give an example from memory because I was on my phone.

So on button press from a userform I run this code:

Code:
If ToolBox.CheckBox_IMPpgs.Value = True Then    Call IMPpgs_Filter
    End If
    If ToolBox.CheckBox_OnPGs.Value = True Then
    Call OnPGs_Filter
    End If
    If ToolBox.CheckBox_Simple.Value = True Then
    Call Simple_Filter
    End If

Here are the calls:

Code:
Sub IMPpgs_Filter()    Dim ProgramName As Range
    
    For Each ProgramName In Range("A4:A400").Cells
        If ProgramName.Value = "IPG" Then
        ProgramName.EntireRow.Hidden = False
        End If
    Next ProgramName
End Sub
Sub OnPGs_Filter()
    Dim ProgramName As Range
    
    For Each ProgramName In Range("A4:A400").Cells
        If ProgramName.Value = "OPG" Then
        ProgramName.EntireRow.Hidden = False
        End If
    Next ProgramName
End Sub
Sub Simple_Filter()
    Dim ProgramName As Range
    
    For Each ProgramName In Range("A4:A400").Cells
        If ProgramName.Value = "OESIMP" Then
        ProgramName.EntireRow.Hidden = False
        End If
        If ProgramName.Value = "OEALL" Then
        ProgramName.EntireRow.Hidden = False
        End If
    Next ProgramName
End Sub

This goes on for like 30 more checkboxes.
 
Upvote 0
I'm a little slow here:

Your wanting to have several checkboxes on your userform.

And if you select checkbox Name Alpha you want to hide all rows that have Alpha in Column A

And if you also selected Check Box Named Bravo the you want to hide all rows that have Bravo in Column A


Is this correct?

And there may be more.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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