Option Explicit
Sub ColoraCedole_Textomb()
Dim RngCint As Range, cell As Range, Val1 As Long, Val2 As Long, Val As Long, Cliente As String
Dim RngCol As Range, MyC As Range, RngCedole As Range
Set RngCint = Range("a9:a" & 8 + WorksheetFunction.CountA(Range("a9:a30")))
Set RngCol = Range("a34:j34") 'Il Range su cui è fatta l'attribuzione dei colori per i Nominativi
Set RngCedole = Range("B38:K137") 'Il Range con le singole cedole
'Dapprima elimino tutti i colori
RngCedole.Interior.ColorIndex = xlColorIndexAutomatic
'Scorro tutti gli intervalli delle cedole assegnate e applico il colore del Nominativo
For Each cell In RngCint 'L'intervallo delle Cedole assegnate ai Nominativi
Val1 = cell.Offset(, 4).Value 'valore cedola inizile
Val2 = cell.Offset(, 5).Value 'valore cedola finale
Cliente = cell.Offset(, 9).Value ' Nominativo
If cell = "" Or Val1 = 0 Or Val2 = 0 Or Cliente = "" Then _
MsgBox "Impossibile Proseguire. Dati incompleti", vbCritical: Exit Sub
Set MyC = RngCol.Find(Cliente, lookat:=xlWhole)
If MyC Is Nothing Then Set MyC = Range("j34")
RngCedole.Find(Val1, lookat:=xlWhole).Interior.ColorIndex = MyC.Interior.ColorIndex
Val = Val1 + 1
Do
RngCedole.Find(Val, lookat:=xlWhole).Interior.ColorIndex = MyC.Interior.ColorIndex
Val = Val + 1
Loop Until Val > Val2
Next
Range("a34").Calculate
Set RngCint = Nothing
Set RngCol = Nothing
Set RngCedole = Nothing
Set cell = Nothing
Set MyC = Nothing
End Sub |