I am trying to provide a easy search tab for some of my employees in order for them to easily filter through a large data table for specific entries and then copy the row from the master list and past into search tab.
The code below actually works and copies the data from the entries that match, unfortunately "country" is the only criteria that works and the rest do nothing.
What I am trying to do is tweak the code I have written to get all criteria to work and if the criteria is blank in the corresponding search then it ignores that criteria allowing any value in that cell to be copy and pasted. I was thinking adding If Else statements for all of the criteria would do the job but I'm not exactly sure how to properly add If Else statement in VBA for all the strings and tell it to ignore the criteria if it is blank.
Sub search_and_extract_multicriteria()
Dim datasheet As Worksheet 'where is the data copied from
Dim reportsheet As Worksheet 'where is the data pasted to
Dim country As String
Dim SubType As String
Dim ProductName As String
Dim ProductFormula As String
Dim Source As String
Dim Rating As String
Dim finalrow As Integer
Dim i As Integer 'row counter
Set datasheet = Sheet1
Set reportsheet = Sheet3
country = reportsheet.Range("A3").Value
SubType = reportsheet.Range("C3").Value
ProductName = reportsheet.Range("D3").Value
ProductFormula = reportsheet.Range("E3").Value
Source = reportsheet.Range("F3").Value
Rating = reportsheet.Range("G3").Value
reportsheet.Range("A16:K500").ClearContents
datasheet.Select
'finalrow = Cells(Row.Count, 1).End(x1Up).Row
For i = 2 To 500 'finalrow
If Cells(i, 1) = country And Cells(i, 3) = SubType And Cells(i, 4) = ProductName And Cells(i, 5) = ProductFormula And Cells(i, 6) = Source And Cells(i, 2) = TestimonialType And Cells(i, 9) = Rating Then
Range(Cells(i, 1), Cells(i, 11)).Copy 'copy columns 1 to 11 (A to K)
reportsheet.Select
Range("A200").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValuesAndNumberFormats
datasheet.Select
End If
Next i
reportsheet.Select 'this is so that the report sheet is selected when the procedure ends
End Sub
Aucun commentaire:
Enregistrer un commentaire