I have 4 different values, i want to check 4 values with 4 different combination
Code
Dim tot1, tot2, tot3, to4 As Variant
Dim item1, item2, item3, item4 As Variant
For Each item1 In tot1
For Each item2 In tot2
For Each item3 In tot3
For Each item4 In tot4
If item1 = "All" And item2 <> "All" And item3 <> "All" And item4 <> "All" Then '1
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 <> "All" And item2 = "All" And item3 <> "All" And item4 <> "All" Then '2
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 <> "All" And item2 <> "All" And item3 = "All" And item4 <> "All" Then '3
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 <> "All" And item2 <> "All" And item3 <> "All" And item4 = "All" Then '4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 = "All" And item2 = "All" And item3 <> "All" And item4 <> "All" Then '1 & 2
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 = "All" And item2 <> "All" And item3 = "All" And item4 <> "All" Then '1 & 3
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 = "All" And item2 <> "All" And item3 <> "All" And item4 = "All" Then '1 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 <> "All" And item2 = "All" And item3 = "All" And item4 <> "All" Then '2 & 3
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 <> "All" And item2 = "All" And item3 <> "All" And item4 = "All" Then '2 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 <> "All" And item2 <> "All" And item3 = "All" And item4 = "All" Then '3 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 = "All" And item2 = "All" And item3 = "All" And item4 <> "All" Then '1 & 2 & 3
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location <> '" & item4 & "'"
ElseIf item1 = "All" And item2 = "All" And item3 <> "All" And item4 = "All" Then '1 & 2 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department <> '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 = "All" And item2 <> "All" And item3 = "All" And item4 = "All" Then '1 & 3 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division <> '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 <> "All" And item2 = "All" And item3 = "All" And item4 = "All" Then '2 & 3 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company <> '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'"
ElseIf item1 <> "All" And item2 <> "All" And item3 <> "All" And item4 <> "All" Then '1 & 2 & 3 & 4
sSQL = "Insert into table1 Select Distinct Emp_Code from Employee Where Company = '" & item1 & "' and Division = '" & item2 & "' and Department = '" & item3 & "' and Location = '" & item4 & "'"
End If
Next
Next
Next
Next
The above mentioned code is working, but code is very hard for checking the 4 values with combination,
Incase if the checking values is 6 then it will be very difficult to write a combination….
Any other alternative method is available…?
I think you havnt mention the exact structure of your data, otherwise there would have been a better code example, however, try your own code with little modification