Allora, considero due fogli
1) nome "Preventivi"
2) nome "Query"
in un modulo inserisci queste macro
- Codice: Seleziona tutto
Sub CreaElUniv()
Set Ws1 = Worksheets("Preventivi")
Set Ws2 = Worksheets("Query")
Ws1.Range("O1:Q65536").ClearContents
UR1 = Ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
For RR1 = 1 To UR1
Cognome = Ws1.Range("B" & RR1).Value
Modello = Ws1.Range("D" & RR1).Value
Targa = Ws1.Range("E" & RR1).Value
UR1Q = Ws1.Range("Q" & Rows.Count).End(xlUp).Row
If Ws1.Range("Q" & UR1Q).Value <> "" Then UR1Q = UR1Q + 1
For RR1Q = 1 To UR1Q
If Ws1.Range("Q" & RR1Q).Value = Targa Then GoTo SaltaRR1
Next RR1Q
Ws1.Range("O" & UR1Q).Value = Cognome
Ws1.Range("P" & UR1Q).Value = Modello
Ws1.Range("Q" & UR1Q).Value = Targa
SaltaRR1:
Next RR1
End Sub
Private Sub UserForm_Terminate()
UserForm1.Hide
End Sub
Sub FormScript()
UR1Q = Worksheets("Preventivi").Range("Q" & Rows.Count).End(xlUp).Row
Application.EnableEvents = False
UserForm1.ComboBox1.RowSource = "Preventivi!O1:O" & UR1Q
UserForm1.ComboBox1.Value = "Cognome"
UserForm1.ComboBox2.RowSource = "Preventivi!P1:P" & UR1Q
UserForm1.ComboBox2.Value = "Modello"
UserForm1.ComboBox3.RowSource = "Preventivi!Q1:Q" & UR1Q
UserForm1.ComboBox3.Value = "Targa"
Application.Wait (Now + TimeValue("0:00:01"))
Application.EnableEvents = True
UserForm1.Show
End Sub
Nel Vba del foglio "Query" inserisci questo codice
- Codice: Seleziona tutto
Private Sub Worksheet_Activate()
Worksheets("Query").Columns("A:F").ClearContents
CreaElUniv
FormScript
End Sub
Ora crei una Userform (si chiamerà UserForm1)
All'interno crei tre Caselle combinate (ComboBox1, ComboBox2, ComboBox3)
Con tasto destro sulla prima in visualizza codice incolli questa macro
- Codice: Seleziona tutto
Private Sub ComboBox1_Change()
Set Ws1 = Worksheets("Preventivi")
Set Ws2 = Worksheets("Query")
If UserForm1.Visible Then
Ws1.Range("A1:F1").Copy Destination:=Ws2.Range("A1")
UREA = Ws1.Range("A" & Rows.Count).End(xlUp).Row
Cognome = Me.ComboBox1
For RRA = 2 To UREA
If Cognome = Ws1.Range("B" & RRA).Value Then
Ws1.Range("A" & RRA & ":F" & RRA).Copy Destination:=Ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
UserForm1.Hide
End If
Next RRA
End If
End Sub
Nella seconda questo codice
- Codice: Seleziona tutto
Private Sub ComboBox2_Change()
Set Ws1 = Worksheets("Preventivi")
Set Ws2 = Worksheets("Query")
If UserForm1.Visible Then
Ws1.Range("A1:F1").Copy Destination:=Ws2.Range("A1")
UREA = Ws1.Range("A" & Rows.Count).End(xlUp).Row
Modello = Me.ComboBox2
For RRA = 2 To UREA
If Modello = Ws1.Range("D" & RRA).Value Then
Ws1.Range("A" & RRA & ":F" & RRA).Copy Destination:=Ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
UserForm1.Hide
End If
Next RRA
End If
End Sub
e nella terza questo
- Codice: Seleziona tutto
Private Sub ComboBox3_Change()
Set Ws1 = Worksheets("Preventivi")
Set Ws2 = Worksheets("Query")
If UserForm1.Visible Then
Ws1.Range("A1:F1").Copy Destination:=Ws2.Range("A1")
UREA = Ws1.Range("A" & Rows.Count).End(xlUp).Row
Targa = Me.ComboBox3
For RRA = 2 To UREA
If Targa = Ws1.Range("E" & RRA).Value Then
Ws1.Range("A" & RRA & ":F" & RRA).Copy Destination:=Ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
UserForm1.Hide
End If
Next RRA
End If
End Sub
Ora se i campi rispettano ciò che hai detto nel primo post
e hai dei dati nelle colonne A:F
al momento che passerai nel foglio Query
si aprirà una userform che ti permetterà di scegliere un Cognome, un modello o una traga
non appena scelta il foglio query si popolerà di dati relativi a ciò che hai inserito nel combobox
Allego il file esempio completo
ciao