ricerca con varie condizioni



  • ricerca con varie condizioni
    di gagen (utente non iscritto) data: 03/04/2013 23:23:51

    buonasera a tutti
    rieccomi da voi!

    con il vostro aiuto ho realizzato un codice che fa il lavoro di ricerca e copia perfettamente. ora vorrei ampliarlo con più scelte

    vorrei poter scegliere queste possibilità di ricerca della riga da copiare
    cbox1+cbox2+cbo4+ una delle 2 tbox oppure
    cbox1+cbox2+ una delle 2 tbox oppure
    cbox1+ una delle 2 tbox (codice attualmente in uso) oppure
    cbox1+cbox2 oppure
    cbox1+cbox4 oppure
    cbox1

    spero non sia troppo complicato
    allego file attuale di esempio
    come sempre grazie mille



  • di Vecchio Frac data: 04/04/2013 21:05:48

    Ci hai provato? Intendo: hai provato sulla falsariga dell'esempio che ti ho indicato io? cioè individuare le celle legando con And le diverse condizioni.





  • di gagen data: 04/04/2013 21:28:02

    spiegami un po'
    la tua soluzione con 2 valori trovati (cbox1 e una delle due tbox) funziona e la ho inserita nel codice.

    se ho capito bene devo aggiungere tante and quante sono le condizioni?

    nelle prove che ho fatto, il codice le scrive tutte, mentre io vorrei avere solo la copia delle righe nelle quali la ricerca trova tutti i valori inseriti in uform (siano essi 1 oppure 2 ecc ecc come da schema sopra)

    il codice è quello del file originale che uso, non modificato per il file allegato, ma spero renda l'idea

    grazie
     
    Option Compare Text 'nessuna diff tra maius e minus
    Option Explicit
      Dim sha As Worksheet, shr As Worksheet
      Dim c As Range, d As Range, E As Range
    
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode <> 1 Then
        MsgBox ("Usa il pulsante CHIUDI per terminare il lavoro"), vbInformation
        Cancel = True
    End If
    End Sub
    
    Private Sub CommandButton1_Click()
    Dim N1 As String, n2 As String, n3 As String
    Dim Ro As Integer
    Set shr = Worksheets("ricerca")
    Set sha = Worksheets("archivio")
    Application.ScreenUpdating = False
    On Error Resume Next
    N1 = TextBox1.Text
    n2 = TextBox2.Text
    n3 = ComboBox2.Value
    Ro = 2
    shr.Activate
    shr.Range("A2:V3000").ClearContents
    If ComboBox1 = "" Then
    MsgBox ("ATTENZIONE CAMPO UFFICIO OBBLIGATORIO"), vbCritical
    Exit Sub
    End If
    '''''''''''''''''''''''''''''''''
    If TextBox2 <> "" And TextBox1 <> "" Then
    MsgBox ("ATTENZIONE   SCEGLIRE SOLO UN NOME")
    Exit Sub
    Else
    For Each c In sha.Range("E:E")
            If c.Value = "" Then Exit For
            If c.Value = N1 And c.Offset(, 2) = ComboBox1 And c.Offset(, 3) = ComboBox2 Then
            Range(c.Offset(0, -4), c.Offset(0, 17)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            Else
            If c.Value = N1 And c.Offset(, 2) = ComboBox1 Then
            Range(c.Offset(0, -4), c.Offset(0, 17)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            End If
            End If
    Next c
    End If
    shr.Activate
    shr.Cells(2, 1).Select
    If shr.Cells(2, 1) <> "" Then
    GoTo 10
    Else
    For Each d In sha.Range("F:F")
            If d.Value = "" Then Exit For
            If d.Value = n2 And d.Offset(, 1) = ComboBox1 And c.Offset(, 3) = ComboBox2 Then
            Range(d.Offset(0, -5), d.Offset(0, 16)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            Else
            If d.Value = n2 And d.Offset(, 1) = ComboBox1 Then
            Range(d.Offset(0, -5), d.Offset(0, 16)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            End If
            End If
    Next d
    End If
    
    If shr.Cells(2, 1) <> "" Then
    GoTo 10
    Else
    If shr.Cells(2, 1) = "" Then
    For Each E In sha.Range("H:H")
            If E.Value = "" Then Exit For
            If E.Value = n3 And E.Offset(, -1) = ComboBox1 And c.Offset(, 3) = ComboBox2 Then
            Range(E.Offset(0, -7), E.Offset(0, 14)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            Else
            If E.Value = n3 And E.Offset(, -1) = ComboBox1 Then
            Range(E.Offset(0, -7), E.Offset(0, 14)).Copy
            shr.Cells(Ro, 1).Select
            Selection.PasteSpecial Paste:=xlPasteValues
            Application.CutCopyMode = False
            Ro = Ro + 1
            End If
            End If
    Next E
    End If
    End If
    '''''''''''''''''''''''''''''''''''''''''''
    10:
    If shr.Cells(2, 1) = "" Then
    MsgBox "Nessuna corrispondenza", vbInformation
    Else
    MsgBox "Ricerca effettuata con successo", vbInformation
    ComboBox3.RowSource = "ricerca!a2:a1500"
    End If
    Exit Sub
    shr.Cells(1, 1).Select
    End Sub
    
    Private Sub ComboBox1_AfterUpdate()
    Dim x As Long, R As Long, ind, SH As Worksheet, xx As Long, XXX As Long
    Set SH = Worksheets("VIARIO")
    ind = ActiveSheet.Name
    If ComboBox1 = "" Then Exit Sub
    Application.ScreenUpdating = False
    SH.Activate
    For x = 3 To 8
       If SH.Cells(1, x) = ComboBox1 Then
            R = SH.Cells(Rows.Count, x).End(xlUp).Row
           ComboBox2.RowSource = SH.Range(Cells(2, x), Cells(R, x)).Address
         Exit For
        End If
    Next x
    For xx = 27 To 31
       If SH.Cells(1, xx) = ComboBox1 Then
            R = SH.Cells(Rows.Count, xx).End(xlUp).Row
           ComboBox4.RowSource = SH.Range(Cells(2, xx), Cells(R, xx)).Address
         Exit For
        End If
    Next xx
    Sheets(ind).Select
    Application.ScreenUpdating = True
    End Sub
    



  • di Vecchio Frac data: 05/04/2013 11:36:57

    Tu stesso ti sei dato la soluzione.

    cbox1+cbox2+cbo4+ una delle 2 tbox oppure
    cbox1+cbox2+ una delle 2 tbox oppure
    cbox1+ una delle 2 tbox (codice attualmente in uso) oppure
    cbox1+cbox2 oppure
    cbox1+cbox4 oppure
    cbox1


    Dove scrivi "+" usi un operatore And, dove scrivi "oppure" usi l'operatore "Or"