Sub Grigliato()
Dim i As Long, Righe As Integer, Colonne As Integer, L As Double, H As Double
Dim Prezzo As Double, Foglio As String, Colore As String, Vuote1 As Integer, Vuote2 As Integer
Dim Inizio_Tab As Long, RigaOk As Integer, ColOk As Integer, Valore As Double
Vuote1 = Application.WorksheetFunction.CountBlank(Range("A4:B4"))
Vuote2 = Application.WorksheetFunction.CountBlank(Range("E4:F4"))
If (Vuote1 + Vuote2) > 0 Then Exit Sub
Foglio = Range("A4").Value
Colore = Range("B4").Value
L = Range("E4").Value
H = Range("F4").Value
With Worksheets(Foglio)
For i = 1 To .UsedRange.Rows.Count
If .Range("C" & i) = Colore Then
Inizio_Tab = .Range("C" & i).Row
End If
Next i
Righe = .Range("C" & Inizio_Tab).End(xlDown).Row
Colonne = .Range("C" & Inizio_Tab + 1).End(xlToRight).Column
For i = Inizio_Tab + 1 To Righe
If .Cells(i, 3).Value < H Then
RigaOk = i
Valore = .Cells(i, 3).Value
End If
Next i
If Valore = 0 Then
RigaOk = Inizio_Tab + 2
ElseIf H > Valore + 5 Then
If RigaOk + 1 <= Righe Then
RigaOk = RigaOk + 1
Else
RigaOk = Righe
End If
End If
Valore = 0
For i = 4 To Colonne
If .Cells(Inizio_Tab + 1, i).Value < L Then
ColOk = i
Valore = .Cells(Inizio_Tab + 1, i).Value
End If
Next i
If Valore = 0 Then
ColOk = 4
ElseIf L > Valore + 5 Then
If ColOk + 1 <= Colonne Then
ColOk = ColOk + 1
Else
ColOk = Colonne
End If
End If
Prezzo = .Cells(RigaOk, ColOk).Value
End With
Range("G4").Value = Prezzo
End Sub