Originally Posted by
mdmackillop
[vba]Private Sub CmdVediPrezzi_Click()
Dim NumRiga2 As Integer, MioCantiere, RngCantieri, Contatore As Integer, PrimaRiga As Integer, ultimariga As Integer, i
Application.ScreenUpdating = False
Worksheets("PrezziPerCantiere").Activate
If ActiveSheet.FilterMode = True Then ActiveSheet.ShowAllData
NumRiga2 = Cells(Rows.Count, 1).End(xlUp).Row
Set RngCantieri = Sheets("PrezziPerCantiere").Range("A2:A" & NumRiga2 + 1)
MioCantiere = TxtCantiere.Text
Contatore = 0
'Why not test from the bottom for the fisrt match, instead of from the top for the last
For Each MioCantiere In RngCantieri
If Not MioCantiere Is Nothing And MioCantiere = TxtCantiere.Text Then
MioCantiere.Select
ultimariga = MioCantiere.Row
End If
Next MioCantiere
For i = ultimariga To 2 Step -1
If Cells(i, 1) = TxtCantiere.Text Then
Contatore = Contatore + 1
Else: Exit For
End If
Next
PrimaRiga = ultimariga - Contatore + 10
If ultimariga > 0 And PrimaRiga > 0 Then
Dim r As Range
Set r = Range("B" & PrimaRiga, "B" & ultimariga)
If r.Count = 1 Then
CmbMateriali.AddItem r.Value
CmbPrezzo.AddItem r.Offset(, 1).Value
CmbData.AddItem r.Offset(, 2).Cells.Value
Else
CmbMateriali.List = Range("B" & PrimaRiga, "B" & ultimariga).Cells.Value
CmbPrezzo.List = Range("C" & PrimaRiga, "C" & ultimariga).Cells.Value
CmbData.List = Range("D" & PrimaRiga, "D" & ultimariga).Cells.Value
End If
Else
MsgBox "Non ci sono prezzi salvati per questo cantiere"
End If
End Sub[/vba]