Search code examples
excelvbarowdropdowncopy-paste

Copy paste rows meeting conditions stated from a multiple selection in a drop-down list (with no repetition)


I have:

1) one worksheet named "Data" for my products data base;

2) one worksheet named "Quotation ENG" for products quotations based on selected products from the data base;

3) one worksheet named "Manager" with dropdown lists to make the criteria selections.

Then, I have two pieces of code running fine independently.

One named Sub Quote is for copy-pasting part of the rows from my database to the quotation sheet when a criterion is met,

And one named Sub Worksheet_Change (credits: TrumpExcel) is for enabling multiple selections in a dropdown list.

I'm quite clueless on how to change the code of my Sub Quote module to make the copy-paste operation possible if the dropdown lists enable multiple criteria. ANy guidance is welcome :)

enter image description here

Sub Quote()

Dim Source As Worksheet
Dim Target As Worksheet
Dim Company As String
Dim InfoA As String
Dim Finalrow As Integer
Dim I As Integer

Set Source = Worksheets("Data")
Set Target = Worksheets("Quotation ENG")
Company = Worksheets("Manager").Range("E5").Value 'Where one dropdown list is located.
InfoA = Worksheets("Manager").Range("E7").Value 'Where one dropdown list is located.

Source.Select
Finalrow = Cells(Rows.Count, 1).End(xlUp).Row

For I = 2 To Finalrow
    If Cells(I, 1) = Company And Cells(I, 2) = InfoA Then
    Source.Range(Cells(I, 16), Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
    End If

Next I

Target.Select
Range("A1").Select

End Sub

=============================================================

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Oldvalue As String
Dim Newvalue As String

Application.EnableEvents = True
On Error GoTo Exitsub

If Target.Address = "$E$5" Then
  If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
    GoTo Exitsub
  Else: If Target.Value = "" Then GoTo Exitsub Else
    Application.EnableEvents = False
    Newvalue = Target.Value
    Application.Undo
    Oldvalue = Target.Value
      If Oldvalue = "" Then
        Target.Value = Newvalue
      Else
        If InStr(1, Oldvalue, Newvalue) = 0 Then
            Target.Value = Oldvalue & ", " & Newvalue
      Else:
        Target.Value = Oldvalue
      End If
    End If
  End If
End If

Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True

End Sub

Solution

  • I refactored some parts of your code and turned the company variable into an array so it can store several values. Please read the comments inside the code.

    As a suggestion, try to use Excel structured tables to store your data. It's gonna be easier to work with them in the future.

    Replace your current Quote Sub for this:

    Sub Quote()
    
        Dim Source As Worksheet
        Dim Target As Worksheet
        Dim Company() As String ' Converted the company variable to an array
        Dim InfoA As String
        Dim Finalrow As Integer
        Dim counter As Integer
        Dim I As Integer
    
        Set Source = Worksheets("Data")
        Set Target = Worksheets("Quotation ENG")
        Company = Split(Worksheets("Manager").Range("E5").Value, ",") 'Where one dropdown list is located.
        InfoA = Worksheets("Manager").Range("E7").Value 'Where one dropdown list is located.
    
        ' Added the source sheet and removed the select as it slows down your code
        Finalrow = Source.Cells(Rows.Count, 1).End(xlUp).Row
    
        ' Loop through each company contained in the array
        For counter = 0 To UBound(Company)
            ' Loop through each data row
            For I = 2 To Finalrow
                ' Added Company(counter) so you can access each array element and wrapped it with trim to delete extra spaces
                If Source.Cells(I, 1) = Trim(Company(counter)) And Source.Cells(I, 2) = InfoA Then
                    Source.Range(Source.Cells(I, 16), Source.Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
                End If
            Next I
        Next counter
    
    
        ' Activate worksheet
        Target.Activate
        ' Refer to the object full path
        Target.Range("A1").Select
    
    End Sub
    

    Let me know if it works.