Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) 'Doppio click
On Error GoTo errore 'Se va in error attiva routine "errore"
If Not Application.Intersect(Target, Range("E12:E500")) Is Nothing Then 'Scelta del range doppio click
Workbooks("InfoGara.xlsx").Activate 'Attiva file "InfoGara"
Sheets(Target.Text).Select ' se il foglio esiste lo seleziona altrimeni va a "errrore"
Exit Sub 'Esci dalla macro
errore: 'Specifica routine "errore" in caso di error
If Target.Text = "" Then Exit Sub 'Se la cella cliccata è vuota esci dalla macro
End If 'Fine funzione If
Application.DisplayAlerts = False ' Evita gli alert, hai dei nomi nel foglio base e verranno duplicati
Sheets("Base").Copy After:=Sheets(Sheets.Count) ' Copia "base" e lo posiziona in fondo
Sheets(Sheets.Count).Name = Target.Text ' Rinomina il foglio copiato
Application.DisplayAlerts = True ' Ripristina gli alert
End Sub 'Fine Macro
Private Sub Worksheet_Change(ByVal Target As Range) 'Cambio cella
Dim KeyCells As Range 'Variabile keycells
Set KeyCells = Range("c14:c500") 'Imposto range
Application.ScreenUpdating = False 'Toglie visione macro
'1. Settaggio formati (per impedire differenze di formato sugli spostamenti delle prossime macro)
Dim wk2 As Workbook
Dim sh2 As Worksheet
Dim lng2 As Long
Set wk2 = ThisWorkbook
Set sh2 = wk2.Worksheets("calendario") 'Variabili varie (da sistemare, si ripetono 3 volte!)*
With sh2 'Ciclo for per settaggio formato
For lng2 = .Range("c" & .Rows.Count).End(xlUp).Row To 16 Step -1
If .Cells(lng2, 3) <> .Cells(lng2 - 1, 3) And .Cells(lng2, 3) <> "" And .Cells(lng2, 3).Offset(0, 1) = "" Then
Rows("14:14").Select
Selection.Copy
.Cells(lng2, 3).EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next
End With
Set sh2 = Nothing 'Da capire
Set wk2 = Nothing 'Da capire
'2. Elimina righe vuote
ThisWorkbook.Sheets("calendario").Select
Range("c14:c500").SpecialCells(xlCellTypeBlanks).EntireRow.Delete 'Seleziona range
'3. Ordina per data
Dim WS As Worksheet
Set WS = ThisWorkbook.Worksheets("calendario")
Dim R As Range
Set R = WS.Range("c14:n500") 'Variabili
With WS.Sort 'Celle che si ordineranno (Insieme in base alla prima ("c"))
.SortFields.Clear
.SortFields.Add Key:=Range("c14:c500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("d14:d500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("e14:e500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("f14:f500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("g14:g500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("h14:h500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("i14:i500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("j14:j500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("k14:k500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("l14:l500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("m14:m500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("n14:n500"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange R
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
'4.Inserimento righe vuote e sistemazione (estetica del foglio)
Dim wk As Workbook
Dim sh As Worksheet
Dim lng As Long
Set wk = ThisWorkbook
Set sh = wk.Worksheets("calendario") 'Variabili (*da sistemare)
With sh
For lng = .Range("c" & .Rows.Count).End(xlUp).Row To 16 Step -1
If .Cells(lng, 3) <> .Cells(lng - 1, 3) And .Cells(lng, 3) <> "" And .Cells(lng, 3).Offset(0, 1) <> "" Then
.Cells(lng, 3).EntireRow.Insert Shift:=xlDown
.Cells(lng, 3).EntireRow.Insert Shift:=xlDown
Else
If .Cells(lng, 3) <> .Cells(lng - 1, 3) And .Cells(lng, 3) <> "" And .Cells(lng, 3).Offset(0, 1) = "" Then
.Cells(lng, 3).EntireRow.Insert Shift:=xlDown
.Cells(lng, 3).Offset(2, 0).EntireRow.Delete
.Cells(lng, 3).Offset(2, 0).EntireRow.Delete
.Cells(lng, 3).EntireRow.Copy
.Cells(lng, 3).Offset(2, 0).EntireRow.Select
Selection.Insert Shift:=xlDown
End If
End If
Next
End With
Set sh = Nothing
Set wk = Nothing 'Non so
'5.Settaggio formato mesi come prima
Dim wk1 As Workbook
Dim sh1 As Worksheet
Dim lng1 As Long
Set wk1 = ThisWorkbook
Set sh1 = wk1.Worksheets("calendario") '*Le solite variabili
With sh1
For lng1 = .Range("c" & .Rows.Count).End(xlUp).Row To 16 Step -1
If .Cells(lng1, 3) <> .Cells(lng1 - 1, 3) And .Cells(lng1, 3) <> "" And .Cells(lng1, 3).Offset(0, 1) = "" Then
Rows("12:12").Select
Selection.Copy
.Cells(lng1, 3).EntireRow.Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next
End With
Set sh1 = Nothing
Set wk1 = Nothing
'6.Selezione per eliminare il tratteggio del tagli sullo schermo e la selezione cella
Cells(600, 1).EntireRow.Copy
Range("a1").Select
Application.ScreenUpdating = True 'Riavvia la visione
End Sub |