Private Sub avvia_ricerca()
Dim foglio_scheda As Worksheet, foglio As Worksheet
Dim cell As Range, area_dati As Range, c As Range, last_row As Long
Dim i As Integer, r As Integer, Uriga As Long, n As Long, Tot As Long, x As Long, rg As Long
Dim cerca(1 To 5) As String, cerca2 As String, cerca3 As String, cerca4 As String, cerca5 As String
Set foglio_scheda = Sheets("scheda")
foglio_scheda.Activate
Uriga = foglio_scheda.Range("A" & Rows.Count).End(xlUp).Row
If Uriga >= 7 Then
foglio_scheda.Range("A7:G" & Uriga).Clear
End If
cerca(1) = [merce].Find(ComboBox1).Offset(, 1) & "tot" '1° termine di ricerca = codice 1° dato + "tot"
cerca(2) = [merce].Find(ComboBox1).Offset(, 1) & IIf(ComboBox2 = "vuoto", "", ComboBox2) '2° termine di ricerca = codice 1° dato + 2° dato
cerca(3) = [merce].Find(ComboBox1).Offset(, 1) & IIf(ComboBox3 = "vuoto", "", ComboBox3) '3° termine di ricerca = codice 1° dato + 3° dato
cerca(4) = IIf(ComboBox4 = "vuoto", "", ComboBox4) '4° termine di ricerca = 4° dato
cerca(5) = IIf(ComboBox5 = "vuoto", "", ComboBox5) '5° termine di ricerca = 5° dato
For i = 1 To 5
Cells(3, i) = cerca(i)
Next
If [COUNTA(A3:E3)] = 0 Then
MsgBox "Non hai inserito alcun termine di ricerca!", vbInformation, "Fine della procedura"
Exit Sub
End If
Application.ScreenUpdating = False
last_row = 7
For Each foglio In Worksheets 'passa in esame foglio per foglio
If LCase(Left(foglio.Name, 4)) = "all_" Then 'ma considera solo i fogli che iniziano con "all_"
Uriga = foglio.Range("F" & Rows.Count).End(xlUp).Row
'copia l'intestazione del foglio corrente
foglio.[A1].Copy foglio_scheda.Cells(last_row, 1)
last_row = last_row + 2
foglio.[F3:H3].Copy foglio_scheda.Cells(last_row, 1)
last_row = last_row + 1 'si posiziona sulla prima riga vuota
r = 0
For i = 1 To 5
n = 64 + i
If cerca(i) <> "" Then
'MsgBox (Chr$(n) & "3:" & Chr$(n) & Uriga)
Tot = Application.WorksheetFunction.CountIf(foglio.Range(Chr$(n) & "3:" & Chr$(n) & Uriga), "*" & cerca(i) & "*")
rg = 3
For x = 1 To Tot
Set area_dati = foglio.Range(Chr$(n) & rg & ":" & Chr$(n) & Uriga)
Set c = area_dati.Find(cerca(i), lookat:=xlPart) 'LookAt:=xlWhole 'xlPart
If Not c Is Nothing Then
r = c.Row ': Exit For
rg = r + 1
foglio.Range(foglio.Cells(r, "F"), foglio.Cells(r, "H")).Copy foglio_scheda.Cells(last_row, 1)
last_row = last_row + 1
End If
Next x
End If
Next i
last_row = last_row + 3
End If
Next
Application.ScreenUpdating = True
Set foglio = Nothing
Set foglio_scheda = Nothing
Set area_dati = Nothing
End Sub |