I have a userform with 3 comboboxes. The code below takes what is selected in each combobox and one at a time filters the data found on sheet (Download) and then copies and paste the filtered data onto another sheet (View).
The code works perfectly if the use makes selections in all 3 comboboxes. However, I need to amend the code so that if the user makes a selection in only combobox1 and leaves 2 and 3 blank the code skip the copy and paste function for the blank comboboxes.
The code works perfectly if the use makes selections in all 3 comboboxes. However, I need to amend the code so that if the user makes a selection in only combobox1 and leaves 2 and 3 blank the code skip the copy and paste function for the blank comboboxes.
Code:
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
'Place all data from userform1 to Tables
Sheets("Sheet2").Range("aa1").Value = UserForm1.ComboBox1.Value
Sheets("Sheet2").Range("ab1").Value = UserForm1.ComboBox2.Value
Sheets("Sheet2").Range("AC1").Value = UserForm1.ComboBox3.Value
Unload Me
Sheets("View").Range("A4:P10000").ClearContents
'Filter combobox 1
With Sheets("Download").Range("A1:O5000")
.AutoFilter
If Sheets("Sheet2").Range("AA1").Value <> "" Then
.AutoFilter Field:=1, Criteria1:=Sheets("Sheet2").Range("AA1").Value
Else
.AutoFilter Field:=1
End If
'Copy and Paste Filter 1
.Range("A1:P1000").SpecialCells(xlCellTypeVisible).Copy Sheets("View").Range("A3")
'Filter Combobox 2
.AutoFilter
If Sheets("Sheet2").Range("AB1").Value <> "" Then
.AutoFilter Field:=1, Criteria1:=Sheets("Sheet2").Range("AB1").Value
Else
.AutoFilter Field:=1
End If
'find first empty row in database
Dim ws As Worksheet
Set ws = Worksheets("View")
Dim iRow As Long
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'Copy and Paste Filter 2
.Range("A2:P1000").SpecialCells(xlCellTypeVisible).Copy Sheets("View").Cells(iRow, "A")
End With
'Filter combobox 3
With Sheets("Download").Range("A1:O5000")
.AutoFilter
If Sheets("Sheet2").Range("AC1").Value <> "" Then
.AutoFilter Field:=1, Criteria1:=Sheets("Sheet2").Range("AC1").Value
Else
.AutoFilter Field:=1
End If
'find first empty row in database
'Dim ws As Worksheet
'Set ws = Worksheets("View")
'Dim iRow As Long
iRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
'Copy and Paste Filter 2
.Range("A2:P1000").SpecialCells(xlCellTypeVisible).Copy Sheets("View").Cells(iRow, "A")
End With
Sheets("View").Select
Application.ScreenUpdating = True
End Sub