Una famiglia di software per fogli di calcolo Microsoft con strumenti per l'analisi, la creazione di grafici e la comunicazione di dati
Ciao MorenaG,
non avendo a disposizione il file excel ho provato a replicarne la struttura dei due fogli e, sperando che corrisponda alla tua situazione reale, ho provato a scrivere una procedura che legga le righe del foglio "Ct1", e duplichi il foglio "Label" per il numero di "PACKAGES" per cui sia presente un valore "QUANTITA'" assegnando ai nuovi fogli il nome del "PACKAGES".
Vedi se restituisce quanto desiderato e se ti può essere da spunto per il tuo lavoro: File esempio
Questo il codice VBA presente nel Modulo1:
Option Explicit
Sub CreaLabel()
Dim wsCt1 As Worksheet
Dim wsLabel As Worksheet
Dim rngDati As Range
Dim r As Range
Dim sNomeArticolo As String
Dim dSPESSORE As Double
Dim sPACKAGES As String
Dim dQUANTITY_PCE As Double
Dim dQUANTITY_CBM As Double
Dim NewLabel As Worksheet
Application.ScreenUpdating = False
With ThisWorkbook
Set wsCt1 = .Worksheets("Ct1")
Set wsLabel = .Worksheets("Label")
End With
With wsCt1
Set rngDati = Intersect(.UsedRange, .Columns("A:G"), .Rows("7:1048576"))
For Each r In rngDati.Columns(1).Cells
If r.MergeArea.Columns.Count = .Columns("A:G").Columns.Count Then
sNomeArticolo = r.Value
Else
If r.Offset(0, 5).Value <> "QUANTITA'" And r.Offset(0, 5).Value <> "" Then
sPACKAGES = r.Value
dSPESSORE = r.Offset(0, 2).Value
dQUANTITY\_PCE = r.Offset(0, 5).Value
dQUANTITY\_CBM = r.Offset(0, 6).Value
If Not SheetExists(sPACKAGES) Then
wsLabel.Copy After:=Sheets(Sheets.Count)
Set NewLabel = Worksheets(Sheets.Count)
NewLabel.Name = sPACKAGES
Else
Set NewLabel = Worksheets(sPACKAGES)
End If
With NewLabel
.Range("B16").Value = sNomeArticolo
.Range("C16").Value = dSPESSORE
.Range("A20").Value = sPACKAGES
.Range("B20").Value = dQUANTITY\_PCE
.Range("C20").Value = dQUANTITY\_CBM
End With
End If
End If
Next r
End With
wsCt1.Activate
Application.ScreenUpdating = True
End Sub
Public Function SheetExists(sSheetName As String, _
Optional ByVal Wb As Workbook) As Boolean
'by Norman Devid Jones
On Error Resume Next
If Wb Is Nothing Then
Set Wb = ThisWorkbook
End If
SheetExists = CBool(Len(Wb.Sheets(sSheetName).Name))
End Function