Optimize If...Else statement

Digitborn.com

Active Member
Joined
Apr 3, 2007
Messages
353
Hi,

Please, help me optimize (make shorter code) the following If...Else statement:

If cmbNamespace.Text <> "" Then
strFilter1 = "Namespace = '" & Trim(cmbNamespace.Text) & "'"
strFilter = strFilter1
Else
strFilter1 = ""
End If
If cmbClass.Text <> "" Then
strFilter2 = "Class = '" & Trim(cmbClass.Text) & "'"
strFilter = strFilter2
Else
strFilter2 = ""
End If
If txtType.Text <> "" Then
strFilter3 = "Type like '%" & txtType.Text & "%'"
strFilter = strFilter3
Else
strFilter3 = ""
End If

If strFilter2 = "" And strFilter3 = "" Then
strFilter = strFilter1
ElseIf strFilter2 <> "" And strFilter3 <> "" Then
strFilter = strFilter2 & " And " & strFilter3
End If
If strFilter1 = "" And strFilter3 = "" Then
strFilter = strFilter2
ElseIf strFilter1 <> "" And strFilter3 <> "" Then
strFilter = strFilter1 & " And " & strFilter3
End If
If strFilter1 = "" And strFilter2 = "" Then
strFilter = strFilter3
ElseIf strFilter1 <> "" And strFilter2 <> "" Then
strFilter = strFilter1 & " And " & strFilter2
End If

If strFilter1 = "" And strFilter2 = "" And strFilter3 = "" Then
strFilter = ""
ElseIf strFilter1 <> "" And strFilter2 <> "" And strFilter3 <> "" Then
strFilter = strFilter1 & " And " & strFilter2 & " And " & strFilter3
End If

You can pass me any variants of statements, loops, number of variables from VB6 or VB.NET.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about...

Code:
Sub Test()
    Dim WFN As WorksheetFunction
    Set WFN = Application.WorksheetFunction
    strFilter1 = "Namespace = '" & Trim(cmbNamespace.Text) & "'"
    strFilter2 = "Class = '" & Trim(cmbClass.Text) & "'"
    strFilter3 = "Type like '%" & txtType.Text & "%'"
    strFilter = strFilter1 & " And " & strFilter2 & " And " & strFilter3
    If cmbNamespace.Text = "" Then
        strFilter = Trim(WFN.Substitute(strFilter, strFilter1 & " And ", ""))
    End If
    If cmbClass.Text = "" Then
        strFilter = Trim(WFN.Substitute(strFilter, " And " & strFilter2, ""))
    End If
    If txtType.Text = "" Then
        strFilter = Trim(WFN.Substitute(strFilter, " And " & strFilter3, ""))
    End If
End Sub
<input id="gwProxy" type="hidden"><!--Session data--><input *******="jsCall();" id="jsProxy" type="hidden">
 
Upvote 0

Forum statistics

Threads
1,214,956
Messages
6,122,465
Members
449,085
Latest member
ExcelError

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