
da inserire nel FOGLIO dove risiede la tabella:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Columns("A:E")) Is Nothing Then
Exit Sub
Else
Application.ScreenUpdating = False
elimina_contorni
r = Mid(Target.Address(0, 0), 2)
On Error Resume Next
With Range(Cells(r, 1), Cells(r, 5)).Borders
.LineStyle = xlContinuos
.ColorIndex = xlAutomatic
End With
Target.Offset(1, 0).Select
End If
Nascondi_e_evidenzia_alternate
Application.ScreenUpdating = True
End Sub
da inserire in un modulo:
Sub elimina_contorni()
With Columns("A:E")
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Interior.ColorIndex = 0
End With
End Sub
Sub Nascondi_e_evidenzia_alternate()
Dim r As Long
Dim righe
Dim ur
Dim Colore
Cells.EntireRow.Hidden = False
ur = ""
For r = 2 To Cells(65536, 1).End(xlUp).Row
If Cells(r, 3).Value = "0" Then
Rows(r).EntireRow.Hidden = True
Else
righe = Cells(r, 1).Value
If r > 1 Then ur = Cells(r - 1, 1).Value
If righe <> ur Or r = 1 Then
If Colore = 6 Then
Colore = 0
Else
Colore = 6
End If
End If
Range(Cells(r, 1), Cells(r, 5)).Interior.ColorIndex = Colore
End If
Next r
End Sub
|
Private Sub Worksheet_Change(ByVal Target As Range)
'se non intercetto le colonne a:f allora non fare nulla...
If Intersect(Target, Columns("A:E")) Is Nothing Then
Exit Sub
'altrimenti...
Else
'con questa apertura evito di far "scrollare" il video
Application.ScreenUpdating = False
'lancio la macro per pulire tutti i contorni precedenti
elimina_contorni
'dall'ultima cella modificata mi ricavo la riga e la colonna
r = Mid(Target.Address(0, 0), 2)
C = Mid(Target.Address(0, 0), 1, 1)
On Error Resume Next
'dispongo il contorno dalla prima alla quinta cella della riga sopra trovata
With Range(Cells(r, 1), Cells(r, 5)).Borders
.LineStyle = xlContinuos
.ColorIndex = xlAutomatic
End With
End If
'lancio la macro per evidenziare le righe
Nascondi_e_evidenzia_alternate
'quindi mi sposto con il cursore: se l'utlima cella modificata era nella
'colonna E allora seleziono la prima riga successiva, altrimenti la cella dopo
If C = "E" Then
Target.Offset(1, -4).Select
Else
Target.Offset(0, 1).Select
End If
Application.ScreenUpdating = True
End Sub
Sub elimina_contorni()
'dalla colonna A alla E elimino tutti i contorni (sopra, sotto, ecc...)
With Columns("A:E")
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Interior.ColorIndex = 0
End With
End Sub
Sub Nascondi_e_evidenzia_alternate()
Dim x As Integer
Dim Rng As Range
'con Rng determino la tabella (dinamica) dei dati...
Cells(1, 1).Select
Set Rng = ActiveCell.CurrentRegion
'... e prima di evidenziare cancello le evidenziature precedenti (=0)
Rng.Offset(1, 0).Resize(Rng.Rows.Count - 1, Rng.Columns.Count).Interior.ColorIndex = 0
'determino il numero dell'ultima riga occupata
fine = Rng.Rows.Count
'prima del ciclo assegno 1 a una variabile
x = 1
'parto dalla seconda riga (salto le intestazioni)
For r = 2 To fine
'se nella colonna C il valore è zero...
If Cells(r, 3).Value = "0" Then
'...allora nascondo la colonna
Rows(r).EntireRow.Hidden = True
'e non la considero nel ciclo
End If
'se la riga non è nascosta allora proseguo il ciclo
If Rows(r).Hidden = False Then
'se la mia variabile è pari assumo il colore giallo ( =6 )
If x Mod 2 = 0 Then
Colore = 6
'altrimenti (quindi numero dispari)
Else
'assumo il colore bianco ( =2 )
Colore = 2
End If
'finite le condizioni aggiungo 1 alla mia varibile
x = x + 1
'assegno il colore appena individuato alla riga
Range(Cells(r, 1), Cells(r, 5)).Interior.ColorIndex = Colore
End If
Next r
End Sub
|
Option Explicit
Private previous_row As Integer
'
Private Sub Worksheet_Change(ByVal Target As Range)
Static previous_row
Dim activeRange As Range
If Target.Column > 5 Then Exit Sub
If Target.Row <> previous_row And previous_row <> 0 Then
Rows(previous_row).Borders.LineStyle = xlNone
End If
If Not (Intersect(Target, [a:e]) Is Nothing) Then
If Target.Row = 1 Then Exit Sub
Set activeRange = Range("A" & Target.Row, "E" & Target.Row)
'Call set_borders:
'per evidenziare le cinque celle della riga modificata ognuna con
'il suo bordino di cella, passare il parametro "single cell"
'per incorniciare la riga di tabella modificata con il solo bordo
'esterno, passare il parametro "entire range"
'per togliere qualunque bordino, non passare alcun parametro
Call set_borders(used_range)
Call set_borders(activeRange, "single cell")
End If
previous_row = Target.Row
If WorksheetFunction.CountBlank(activeRange) = 5 Then
Call set_borders(activeRange, "none")
End If
Application.ScreenUpdating = False
Call hide_rows(used_range)
Call alternate_rows(used_range)
If Target.Rows.Hidden Then
Target.Offset(1, 0).Select
Else
Target.Select
End If
If Target.Column = 5 And Target <> "" Then
Target.Offset(1, -4).Select
End If
Application.ScreenUpdating = True
End Sub
Private Sub set_borders(rng As Range, Optional mode As String)
Dim myBorders() As Variant, item As Variant
Application.ScreenUpdating = False
rng.EntireRow.Borders.LineStyle = xlNone
Select Case LCase(mode)
Case "single cell"
rng.Borders.LineStyle = xlContinuous
rng.Borders.Weight = xlMedium
Case "entire range"
myBorders = Array(xlEdgeLeft, xlEdgeTop, xlEdgeBottom, xlEdgeRight)
For Each item In myBorders
With rng.Borders(item)
.LineStyle = xlContinuous
.Weight = xlMedium
End With
Next
End Select
Application.ScreenUpdating = True
End Sub
Private Sub alternate_rows(rng As Range)
Const GIALLINO = 36, VERDINO = 35
Dim riga As Range, c As Integer, previous_color As Integer
Application.ScreenUpdating = False
'coloro alternativamente di giallino (36) e verdino (35) a partire dalla seconda riga del range,
'evitando le righe già nascoste
previous_color = VERDINO
For Each riga In rng.Rows
riga.Interior.ColorIndex = IIf(previous_color = VERDINO, GIALLINO, VERDINO)
previous_color = riga.Interior.ColorIndex
If riga.Rows.Hidden Then previous_color = IIf(riga.Interior.ColorIndex = VERDINO, GIALLINO, VERDINO)
Next
Application.ScreenUpdating = True
End Sub
Private Sub hide_rows(rng As Range)
Dim riga As Range
Application.ScreenUpdating = False
'nascondo le righe in cui la colonna C è zero o vuota
For Each riga In rng.Rows
riga.EntireRow.Hidden = (riga.Columns(3) = 0) Or (riga.Columns(3) = "") Or (WorksheetFunction.CountBlank(riga) = 5)
Next
Application.ScreenUpdating = True
End Sub
Private Function used_range() As Range
Dim ac As Range, r As Long
Application.ScreenUpdating = False
'determina l'ultima riga contenente almeno un dato
r = 1
For Each ac In [a65536:e65536]
ac.End(xlUp).Select: If ActiveCell.Row > r Then r = ActiveCell.Row
Next
If r < 2 Then r = 2
Set used_range = Range("A2:E" & r)
Application.ScreenUpdating = True
End Function |
