Public Sub Ordina()
On Error GoTo Ordina_Error
ActiveSheet.Unprotect
Dim Record, Campi As Integer
Dim RevocaM, Revoca1, Revoca2, RevocaA, RevocaT As Integer
Dim SceltaM, Scelta1, Scelta2, SceltaA, SceltaT As Integer
Dim Uomini, UnderM, OverM As Integer
Dim Donne, UnderF, OverF As Integer
Dim NomeCampo As Variant
Dim ZonaLavoro As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Worksheets(4).Activate
Path = Range("Percorso").Value
Lista = Range("Lista").Value
Verifica = Range("Verifica").Value
'Annullo i dati di Scelte e Revoche Precedenti
For F = 2 To 3
Workbooks(Verifica).Worksheets(F).Activate
Righe = WorksheetFunction.CountA(Range("A:A"))
Colonne = WorksheetFunction.CountA(Range("1:1"))
Range(Cells(1, 1), Cells(Righe + 1, Colonne + 1)).Select
With Selection
.Clear
.RowHeight = 15
End With
Range("A1").Select
Next F
'Importo la ListaAssistiti
Workbooks.OpenText FileName:= _
Path & "" & Lista, Origin:=xlWindows, _
StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 4), Array(7, 1), Array(8, 1), Array(9, 1), _
Array(10, 4), Array(11, 4), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
16, 1), Array(17, 1)), TrailingMinusNumbers:=True
Workbooks(Lista).Activate
'Verifico il numero dei Record e dei campi
Record = WorksheetFunction.CountA(Range("A:A"))
Campi = WorksheetFunction.CountA(Range("1:1"))
Range(Cells(1, 1), Cells(Record, Campi)).Sort Key1:=Range("K2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
dt_r = Range("K2").Value
'Calcolo le Scelte e le Revoche nei periodi di riferimento
If Workbooks(Verifica).Worksheets(4).Range("Versione_Excel").Value = "11.0" Then
MeseA = EoMonth(Now(), -1)
Mese1 = EDate(Now(), -1)
Mese2 = EDate(Now(), -2)
Mese12 = EDate(Now(), -12)
Else
MeseA = WorksheetFunction.EoMonth(Now(), -1)
Mese1 = WorksheetFunction.EDate(Now(), -1)
Mese2 = WorksheetFunction.EDate(Now(), -2)
Mese12 = WorksheetFunction.EDate(Now(), -12)
End If
RevocaM = WorksheetFunction.CountIf(Range("K:K"), ">" & MeseA)
Revoca1 = WorksheetFunction.CountIf(Range("K:K"), ">" & Mese1)
Revoca2 = WorksheetFunction.CountIf(Range("K:K"), ">" & Mese2)
RevocaA = WorksheetFunction.CountIf(Range("K:K"), ">" & Mese12)
RevocaT = WorksheetFunction.CountA(Range("K:K"))
Range(Cells(1, 1), Cells(Record, Campi)).Sort Key1:=Range("J2"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
dt_a = Range("J2").Value
SceltaM = WorksheetFunction.CountIf(Range("J:J"), ">" & MeseA)
Scelta1 = WorksheetFunction.CountIf(Range("J:J"), ">" & Mese1)
Scelta2 = WorksheetFunction.CountIf(Range("J:J"), ">" & Mese2)
SceltaA = WorksheetFunction.CountIf(Range("J:J"), ">" & Mese12)
SceltaT = WorksheetFunction.CountA(Range("J:J"))
'Creo gli elenchi di Scelte e Revoche
Workbooks(Lista).Activate
Range(Cells(1, 1), Cells(Record, Campi)).Sort Key1:=Range("J2"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
InizioS = WorksheetFunction.Match(WorksheetFunction.VLookup(Mese1, Range(Cells(2, 10), Cells(Record, 10)), 1, True), Range(Cells(1, 10), Cells(Record, 10)), 1)
Range("1:1").Copy Destination:=Workbooks(Verifica).Worksheets("Scelte").Range("A1")
Range(InizioS + 1 & ":" & Record).Copy Destination:=Workbooks(Verifica).Worksheets("Scelte").Range("A2")
Range(Cells(1, 1), Cells(Record, Campi)).Sort Key1:=Range("K2"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
InizioR = WorksheetFunction.Match(WorksheetFunction.VLookup(Mese1, Range(Cells(2, 11), Cells(Record, 11)), 1, True), Range(Cells(1, 11), Cells(Record, 11)), 1)
Range("1:1").Copy Destination:=Workbooks(Verifica).Worksheets("Revoche").Range("A1")
Range(InizioR + 1 & ":" & RevocaT).Copy Destination:=Workbooks(Verifica).Worksheets("Revoche").Range("A2")
'Elimino le revoche dall'elenco generale
Range(2 & ":" & RevocaT).Delete xlShiftUp
'Calcolo le statistiche per sesso ed età
Workbooks(Verifica).Worksheets(1).Activate
If Workbooks(Verifica).Worksheets(4).Range("Versione_Excel").Value = "11.0" Then
'Under_Data = EDate(Now(), -12 * Range("Under").Value)
'Over_Data = EDate(Now(), -12 * Range("Over").Value)
Else
Under_Data = WorksheetFunction.EDate(Now(), -12 * Range("Under").Value)
Over_Data = WorksheetFunction.EDate(Now(), -12 * Range("Over").Value)
End If
Workbooks(Lista).Activate
Range(Cells(1, 1), Cells(SceltaT - RevocaT, Campi)).Sort _
Key1:=Range("E2"), Order1:=xlAscending, _
Key2:=Range("F2"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Uomini = WorksheetFunction.CountIf(Range("E:E"), "M")
Donne = WorksheetFunction.CountIf(Range("E:E"), "F")
ActiveWorkbook.Worksheets.Add After:=Worksheets(1)
ActiveWorkbook.Worksheets(1).Activate
Range("1:1").Copy Destination:=Workbooks(Lista).Worksheets(2).Range("A1")
Range(2 & ":" & Donne + 1).Copy Destination:=Workbooks(Lista).Worksheets(2).Range("A2")
Range(2 & ":" & Donne + 1).Delete xlShiftUp
UnderM = WorksheetFunction.CountIf(Range("F:F"), ">=" & Under_Data)
OverM = WorksheetFunction.CountIf(Range("F:F"), "<=" & Over_Data)
ActiveWorkbook.Worksheets(2).Activate
UnderF = WorksheetFunction.CountIf(Range("F:F"), ">=" & Under_Data)
OverF = WorksheetFunction.CountIf(Range("F:F"), "<=" & Over_Data)
With Workbooks(Verifica).Sheets("Scheda")
.Range("RevocaM").Value = RevocaM
.Range("Revoca30").Value = Revoca1
.Range("Revoca60").Value = Revoca2
.Range("RevocaA").Value = RevocaA
.Range("SceltaM").Value = SceltaM
.Range("Scelta30").Value = Scelta1
.Range("Scelta60").Value = Scelta2
.Range("SceltaA").Value = SceltaA
.Range("DataAGG").Value = Format(WorksheetFunction.Max(dt_a, dt_r), "dd/mm/yyyy")
.Range("Carico").Value = SceltaT - RevocaT
.Range("Uomini").Value = Uomini
.Range("Donne").Value = Donne
.Range("UM").Value = UnderM
.Range("UF").Value = UnderF
.Range("OM").Value = OverM
.Range("OF").Value = OverF
End With
Workbooks(Lista).Close SaveChanges:=False
Workbooks(Verifica).Worksheets(4).Activate
NrCampi = WorksheetFunction.CountA(Range("A:A"))
'Formatta Il foglio delle Scelte e Revoche
For F = 2 To 3
Workbooks(Verifica).Worksheets(F).Activate
Record = WorksheetFunction.CountA(Range("A:A"))
Campi = WorksheetFunction.CountA(Range("1:1"))
Set ZonaLavoro = Range(Cells(1, 1), Cells(Record, Campi))
ZonaLavoro.Sort Key1:=Cells(1, 8 + F), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
I = 1
Workbooks(Verifica).Worksheets(4).Activate
CampiF = WorksheetFunction.CountA(Range("C:C"))
Do Until Worksheets(F).Cells(1, I).Value = ""
NomeCampo = Worksheets(F).Cells(1, I).Value
If WorksheetFunction.VLookup(NomeCampo, Worksheets(4).Range(Cells(2, 1), Cells(NrCampi, 3)), 2, False) = "N" Then
Worksheets(F).Columns(I).Delete Shift:=xlToLeft
Else
Worksheets(F).Cells(1, I).Value = WorksheetFunction.VLookup(NomeCampo, Worksheets(4).Range(Cells(2, 1), Cells(NrCampi, 3)), 3, False)
I = I + 1
End If
Loop
Workbooks(Verifica).Worksheets(F).Activate
Range("A1:B1").Merge
Range(Cells(1, 1), Cells(1, CampiF)).Select
With Selection
.Font.Bold = True
.RowHeight = 25
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.Interior.ColorIndex = 15
.Interior.Pattern = xlSolid
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
End With
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
Cells.EntireColumn.AutoFit
Rows(2 & ":" & Record).Select
With Selection
.RowHeight = 25
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
End With
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.PrintArea = ""
.LeftHeader = ""
.CenterHeader = "&""Arial,Grassetto""&12&A"
.RightHeader = ""
.LeftFooter = ""
.CenterFooter = "&P di &N"
.RightFooter = "Dati Aggiornati al: " & Format(WorksheetFunction.Max(dt_a, dt_r), "dd/mm/yyyy")
.LeftMargin = Application.InchesToPoints(0.393700787401575)
.RightMargin = Application.InchesToPoints(0.393700787401575)
.TopMargin = Application.InchesToPoints(0.984251968503937)
.BottomMargin = Application.InchesToPoints(0.984251968503937)
.HeaderMargin = Application.InchesToPoints(0.511811023622047)
.FooterMargin = Application.InchesToPoints(0.511811023622047)
.PrintHeadings = False
.PrintGridlines = True
.PrintComments = xlPrintNoComments
'.PrintQuality = 600
.CenterHorizontally = True
.CenterVertically = False
.Orientation = xlLandscape
.Draft = False
.PaperSize = xlPaperA4
.FirstPageNumber = xlAutomatic
.Order = xlDownThenOver
.BlackAndWhite = False
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintErrors = xlPrintErrorsDisplayed
End With
ZonaLavoro.Select
ActiveWindow.Zoom = True
Range("A1").Select
Next F
Workbooks(Verifica).Worksheets(1).Activate
Ordina_Exit:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveSheet.EnableSelection = xlUnlockedCells
Exit Sub
Ordina_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedura Ordina del Modulo Modulo1"
Resume Ordina_Exit
End Sub
|