Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
La risposta è stata tradotta automaticamente. Di conseguenza, potrebbero esserci errori grammaticali o parole insolite.
Carissimo, GiuseppeXG
Naturalmente,! Sono contento che funzioni, se hai bisogno di automatizzare questo processo e hai bisogno di un altro metodo, inserisci questo codice direttamente nel tuo foglio di calcolo. Come mostra l'immagine:
New Code here:
Private Sub Worksheet_Change(ByVal Target As Range)
' Define the range where changes are monitored
Dim WatchRange As Range
Dim IntersectRange As Range
Set WatchRange = Me.Range("A13:A20")
Set IntersectRange = Intersect(Target, WatchRange)
' Check if the changed cell is within the range of interest
If Not IntersectRange Is Nothing Then
Call HideEmptyRows
End If
End Sub
Sub HideEmptyRows()
Dim rng As Range
Dim cell As Range
Dim firstEmptyFound As Boolean
Set rng = Me.Range("A13:A20") ' Define your range
Application.EnableEvents = False ' Disable events to prevent endless loops
firstEmptyFound = False ' Flag to keep track of the first empty cell
For Each cell In rng
If Len(cell.Value) = 0 Then ' If the cell is empty
If firstEmptyFound Then
cell.EntireRow.Hidden = True ' Hide the row if it's not the first empty cell
Else
firstEmptyFound = True ' Mark the first empty cell found
cell.EntireRow.Hidden = False ' Ensure the row with the first empty cell is not hidden
End If
Else
cell.EntireRow.Hidden = False ' Unhide the row if the cell is not empty
End If
Next cell
Application.EnableEvents = True ' Re-enable events after the updates
End Sub
Questo script modifica l'evento Worksheet_Change per chiamare HideEmptyRows quando si verifica una modifica in una delle celle comprese tra A13 e A20. Il comando Application.EnableEvents = False disabilita temporaneamente la gestione degli eventi per evitare potenziali cicli infiniti quando si modifica il contenuto delle celle a livello di codice da VBA (ad esempio, nascondere/visualizzare le righe).
Chiudi l'editor VBA e torna a Excel. Guarda le immagini (poiché viene fatto automaticamente, puoi vedere solo i risultati dello screenshot):