psbrown
MIS
- Oct 16, 2001
- 40
Hi I am a VBA newbie
I have a userform with 2 combo boxes that I want the user to sellect criteria for a autofilter in Excel, when the comboboxes are populated the user hits a command button to set off the filter.
I want the filter to return all values of that column if that combo box is left blank.
The filter is working when there is data in the first combo box and when there is data in the first and second combo box but does not work when there is no data in either the first combo box or both combo boxes.
I was going to do a "if then" selection on the values of the combo boxes to set off different code for the auto filter but I cannot seem to find code that will test the value of an empty combo box.
Can someone please help me with code for this test or alternativly a better way of coding the autofilter to allow blanks.
Thanks
Paul
The code is below with a few spurious msgboxes to track the execution
Private Sub CmdCalc_Click()
'Set up Variables for Worksheet
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnData As Range
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("FrontSheet")
'Set Range
With wsSheet
Set rnData = .UsedRange
End With
'Assign Values in Form Combobox to variables
If IsEmpty(CmbBusMan.Value) Then
BusMan = "All"
Else
BusMan = CmbBusMan.Value
End If
If IsEmpty(CmbBusSec.Value) Then
BusSec = "All"
Else
BusMan = CmbBusMan.Value
End If
MsgBox (BusMan)
MsgBox (BusSec)
'If BusMan <> "All" And BusSec <> "All" Then
rnData.AutoFilter Field:=5, Criteria1:=BusMan
rnData.AutoFilter Field:=6, Criteria1:=BusSec
Application.ScreenUpdating = True
'End If
'Sheet1.splithours 'Set off procedure to calculate hours
'Unload Me
End Sub
I have a userform with 2 combo boxes that I want the user to sellect criteria for a autofilter in Excel, when the comboboxes are populated the user hits a command button to set off the filter.
I want the filter to return all values of that column if that combo box is left blank.
The filter is working when there is data in the first combo box and when there is data in the first and second combo box but does not work when there is no data in either the first combo box or both combo boxes.
I was going to do a "if then" selection on the values of the combo boxes to set off different code for the auto filter but I cannot seem to find code that will test the value of an empty combo box.
Can someone please help me with code for this test or alternativly a better way of coding the autofilter to allow blanks.
Thanks
Paul
The code is below with a few spurious msgboxes to track the execution
Private Sub CmdCalc_Click()
'Set up Variables for Worksheet
Dim wbBook As Workbook
Dim wsSheet As Worksheet
Dim rnData As Range
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("FrontSheet")
'Set Range
With wsSheet
Set rnData = .UsedRange
End With
'Assign Values in Form Combobox to variables
If IsEmpty(CmbBusMan.Value) Then
BusMan = "All"
Else
BusMan = CmbBusMan.Value
End If
If IsEmpty(CmbBusSec.Value) Then
BusSec = "All"
Else
BusMan = CmbBusMan.Value
End If
MsgBox (BusMan)
MsgBox (BusSec)
'If BusMan <> "All" And BusSec <> "All" Then
rnData.AutoFilter Field:=5, Criteria1:=BusMan
rnData.AutoFilter Field:=6, Criteria1:=BusSec
Application.ScreenUpdating = True
'End If
'Sheet1.splithours 'Set off procedure to calculate hours
'Unload Me
End Sub