Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione dei dati.
Ciao Matteo,
vorrei poter implementare la macro seguente inserendo la possibilità di saltare la creazione del raggruppamento quando trova delle celle vuote, passando quindi alla cella successiva e raggruppare solo se c'è un valore.
Sub sOutlineGrouping()
Set rngCurrent = Range("B8") 'arting cell to create grouping
Do While Not rngCurrent = ("B100000") ' "" '
If rngCurrent <> strPrevValue Then ' if range is different from previous value, it means grouping label has changed, so set the new starting row
lngStartRow = rngCurrent.Row + 1 ' the starting row must be incremented by 1, because to group data you must exclude the first row
End If
strPrevValue = rngCurrent ' store the current value in strPrevValue variable
Set rngCurrent = rngCurrent.Offset(1) ' move to next row
If rngCurrent <> strPrevValue Then ' if next row value is different from previous one, then it is time to group rows
lngEndRow = rngCurrent.Offset(-1).Row ' set ending row, moving back one row
Rows(lngStartRow & ":" & lngEndRow).Group ' finally, group rows
End If
Loop ' restart the loop
End Sub
Come posso fare?
è possibile inoltre applicare la stessa macro a tutti i fogli del file?
Prova qualcosa del genere:
- Alt+F11 per aprire l'editor di VBA
- Alt+IM per inserire un nuovo modulo di codice
- Nel nuovo modulo vuoto, incolla il seguente codice:
'========>>
Option Explicit
'-------->>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim i As Long, r As Long
Dim iRows As Long
Const iPrimaRiga As Long = 8
Set WB = ThisWorkbook
For Each SH In WB.Worksheets
With SH
.UsedRange.ClearOutline
iRows = .Range("B" & .Rows.Count).End(xlUp).Row
For i = iPrimaRiga To iRows
r = 0
Do While .Range("B" & i + r).Value <> "" And Not IsEmpty(.Range("B" & i + r).Value) And .Range("B" & i + r).Value = .Range("B" & i + r - 1).Value
r = r + 1
Loop
.Range("B" & i & ":B" & i + r - 1).Rows.Group
i = i + r
Next i
End With
Next SH
End Sub
'<<========
- Alt+Q per chiudere l'editor di VBA e tornare a Excel.
- Salva il file con l'estensione xlsm
- Alt+F8 per aprire la finestra di gestione delle macro
- Seleziona Tester
- Esegui
===
Regards,
Norman