Question : VBA assistance

Hi,

I am using the code attached to filter 2 listboxes in excel.  It worked for me before, but now, it will not filter and display the results in the 'Analysis' worksheet.  It will only display the complete dataset.  What modifications do I need to make so that the code will filter my dataset?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
Private Sub CommandButton3_Click()

Application.ScreenUpdating = False

Dim ws1 As Worksheet, ws2 As Worksheet, rng As Range, cel As Range, i As Long, sList1 As String, sList2 As String
Dim cCont As Control


Set ws1 = Worksheets("Data")
Set ws2 = Worksheets("Analysis")

sList1 = ""
sList2 = ""

If ListBox1.Selected(0) = True Then
    sList1 = ""
Else
 For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) Then
        sList1 = sList1 & "|" & ListBox1.List(i)
    End If
 Next i
 If sList1 = "" Then
    sList1 = ""
    ListBox1.Selected(0) = True
 Else
    sList1 = sList1 & "|"
 End If
 
End If

If ListBox2.Selected(0) = True Then
    sList2 = ""
Else
 For i = 0 To ListBox2.ListCount - 1
   If ListBox2.Selected(i) Then
      sList2 = sList2 & "|" & ListBox2.List(i)
   End If
 Next i
 If sList2 = "" Then
    sList2 = ""
    ListBox2.Selected(0) = True
 Else
   sList2 = sList2 & "|"
 End If
End If

If ws2.UsedRange.Cells.Count > 1 Then ws2.UsedRange.Clear
ws1.[A1:AX1].Copy Destination:=ws2.[A16]

Set rng = ws1.Range(ws1.[A17], ws1.Cells(ws1.Rows.Count, "A").End(xlUp))

For Each cel In rng
  If UCase(sList1) = "" Or InStr(sList1, "|" & cel.Value & "|") <> 0 Then
     If UCase(sList2) = "" Or InStr(sList2, "|" & cel.Offset(0, 1).Value & "|") <> 0 Then
  cel.Resize(1, 50).Copy Destination:=ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1)
     End If
  End If
Next cel

Unload Me

Application.ScreenUpdating = True

End Sub

Private Sub CommandButton4_Click()
Unload Me
End Sub

Private Sub UserForm1_Initialize()
ListBox1.Selected(0) = True
ListBox2.Selected(0) = True

End Sub

Answer : VBA assistance

I think your code is copying all the rows because of th structure of the "if" clauses below:

For Each cel In rng
  If UCase(sList1) = "" Or InStr(sList1, "|" & cel.Value & "|") <> 0 Then
     If UCase(sList2) = "" Or InStr(sList2, "|" & cel.Offset(0, 1).Value & "|") <> 0 Then
  cel.Resize(1, 50).Copy Destination:=ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1)
     End If
  End If
Next cel

In effect the first "if" picks the row to copy EITHER if slist1 is blank, or if there's a match. The second "if" does the same for slist2.

My guess is what you intended was something like the code below:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
For Each cel In rng
  'if slist1 is blank, check slist2:
  If UCase(sList1) = "" then
     If InStr(sList2, "|" & cel.Offset(0, 1).Value & "|") <> 0 then
     cel.Resize(1, 50).Copy Destination:=ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1)
     end if
  else
  'if slist1 is not blank check for match in slist1
     if InStr(sList1, "|" & cel.Value & "|") <> 0 Then
     cel.Resize(1, 50).Copy Destination:=ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Offset(1)
     end if
end if

Next cel
Random Solutions  
 
programming4us programming4us