1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
Option Explicit
Sub Creation_Onglets()
Dim shtSource As Worksheet, shtTemplate As Worksheet
With ThisWorkbook
Set shtSource = .Worksheets("Sheet N°1"): Set shtTemplate = .Worksheets("Template")
End With
SplitFieldsTableToSheet shtSource, ClearSheet:=False, Template:=shtTemplate
Set shtSource = Nothing: Set shtTemplate = Nothing
If SplitFieldsTableToSheet(shtSource, ClearSheet:=False, Field:="Toto", Template:=shtTemplate) = False Then
MsgBox "Erreur"
End If
End Sub
Function SplitFieldsTableToSheet(SourceData As Object, Optional Field As String, _
Optional ClearSheet As Boolean = True, Optional Template As Worksheet) As Boolean
' Procédure de création de feuilles
' avec exportation de données filtrées par appel de la procédure 'ExportToSheet'
' Author : Philippe Tulliez http://philippe.tulliez.be
' Date : 2013/04/01 (2013/03/22 v 1.0)
' Version : 2.1
' Arguments
' SourceData - (Range) Plage des données à exporter
' [Field] - (String) Nom de l'étiquette de colonne qui contient les données dont on va filtrer les données - Colonne 1 par défaut
' [ClearSheet] - Boolean [d:=True] si False ajoute les lignes exportées derrières les autres
' [Template] - (obj WorkSheet) Feuille modèle / Permet de préparer une mise en page
'
' Déclaration + Initialisation des variables
Const ParamName = "_ParamWrk" ' Feuille paramètre crée dynamiquement
Const Ver As String = "v2.1": Const ErrTitle As String = "Procédure - SplitFieldsTableToSheet " & Ver
Dim wkb As Workbook, rngList As Range, rngSource As Range, rngCriteria As Range, shtParam As Worksheet
Dim ColumnPosition As Integer, r As Long
Dim ErrMsg As String: ErrMsg = "*** Sortie de procédure ***" & vbCrLf & vbCrLf
Application.ScreenUpdating = False
Select Case True ' Test 1er argument
Case TypeOf SourceData Is Worksheet: Set rngSource = SourceData.Range("A1")
Case TypeOf SourceData Is Range: Set rngSource = SourceData
Case Else
MsgBox ErrMsg & "Problème : Objet mal défini (WorkSheet ou Range)", vbCritical, ErrTitle
Exit Function
End Select
If rngSource.Count = 1 Then Set rngSource = SourceData.Range("A1").CurrentRegion
Set wkb = ThisWorkbook
Application.ScreenUpdating = False
' Etape 1 - Création de la feuille paramètre
Do
On Error Resume Next
Set shtParam = wkb.Worksheets(ParamName)
If Err Then wkb.Worksheets.Add.Name = ParamName
On Error GoTo 0
Loop While shtParam Is Nothing
With shtParam
.Cells.Clear
Set rngList = .Range("A1"): Set rngCriteria = .Range("C1:C2")
End With
' Etape 2 - Création d'une liste unique basée sur la colonne du champ (Field) si argument vide colonne 1
With rngSource ' Position du champ (Argument Field)
If Len(Field) > 0 Then
On Error Resume Next
ColumnPosition = Application.WorksheetFunction.Match(Field, .Resize(1), 0) - 1
If Err Then
ErrMsg = ErrMsg & "Field (" & Field & ") not found in SourceData [" & rngSource.Worksheet.Name & "]"
MsgBox ErrMsg, vbCritical + vbOKOnly, ErrTitle
Set rngList = Nothing: Set shtParam = Nothing: Set rngSource = Nothing
Exit Function
End If
Else
ColumnPosition = 0
End If
With .Offset(, ColumnPosition).Resize(, 1)
.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=rngList, Unique:=True ' Exportation des données sans doublons
End With
With shtParam: .Range("C1") = .Range("A1"): End With
End With
' Etape 3 - Boucle qui invoque la procédure d'exportation [ExportToSheet]
For r = 1 To rngList.CurrentRegion.Rows.Count - 1
rngCriteria.Cells(2, 1) = rngList.Offset(r) ' Insère le critère
If ExportToSheet(rngSource, rngCriteria, rngList.Offset(r), ClearSheet:=ClearSheet, TemplateSheet:=Template) = False Then
ErrMsg = ErrMsg & "Problem from [ExportToSheet] with (" & rngList.Offset(r) & ")"
MsgBox ErrMsg, vbCritical + vbOKOnly, ErrTitle
SplitFieldsTableToSheet = False: Exit For
Else
SplitFieldsTableToSheet = True
End If
Next r
' Etape 5 - Destruction de la feuille paramètres
Application.DisplayAlerts = False: shtParam.Delete: Application.DisplayAlerts = True
'
Set wkb = Nothing: Set rngList = Nothing: Set rngSource = Nothing: Set rngCriteria = Nothing
Application.ScreenUpdating = True
End Function
Function ExportToSheet(SourceData As Range, areaCriteria As Range, TargetSheetName As String, _
Optional ClearSheet As Boolean = True, Optional TemplateSheet As Worksheet) As Boolean
' Procédure d'exportation de données filtrées vers une feuille définie par l'arguement 'TargetSheetName'
' - Création de la feuille si elle n'existe pas
' Cette procédure est basée sur la méthode AdvancedFilter de l'objet Range
' Contraintes :
' L'exportation est faite sur le même classeur que SourceData
' La liste exportée commence à A1
' Author : Philippe Tulliez http://philippe.tulliez.be
' Date : 2013/04/03 (2013/03/22 v 1.0)
' Version : 4.1
' Arguments
' SourceData - (Range) Plage des données à exporter
' areaCriteria - (Range) Plage des critères
' TargetSheetName - (String) Nom de la feuille où exporter les données filtrées
' [ClearSheet] - Boolean [d:=True] si False ajoute les lignes exportées derrières les autres
' [TemplateSheet] - (obj WorkSheet) Feuille modèle / Permet de préparer une mise en page
Const Ver As String = "v4.1": Const ErrTitle As String = "Procédure - ExportToSheet " & Ver
Dim ErrMsg As String: ErrMsg = "*** Sortie de procédure ***" & vbCrLf & vbCrLf
Dim shtTarget As Worksheet, rngTarget As Range, rngSource As Range
Dim wkb As Workbook: Set wkb = SourceData.Worksheet.Parent
Dim nbRow As Long, isSheetVisible As Boolean
If SourceData.Count = 1 Then Set rngSource = SourceData.CurrentRegion Else Set rngSource = SourceData
If rngSource.Count = 1 Then
With SourceData: ErrMsg = ErrMsg & "SourceData is strong (" & .Worksheet.Name & "!" & .Address & ")": End With
MsgBox ErrMsg, vbCritical + vbOKOnly, ErrTitle: Exit Function
End If
' Création de la feuille
Select Case TemplateSheet Is Nothing
Case True ' Création feuille
wkb.Sheets.Add Before:=Sheets(1)
Case False
With TemplateSheet
isSheetVisible = .Visible: .Visible = xlSheetVisible
.Copy Before:=Sheets(1) ': Sheets(1).Visible = True
.Visible = isSheetVisible
End With
End Select
On Error Resume Next
wkb.Sheets(1).Name = TargetSheetName
Application.DisplayAlerts = False
If Err Then wkb.Sheets(1).Delete ' Delete NewSheet if TargetSheetName Exist
Application.DisplayAlerts = True
On Error GoTo 0
' Exportation vers nlle feuille suivant critère
Set shtTarget = wkb.Sheets(TargetSheetName): Set rngTarget = shtTarget.Range("A1")
With rngTarget
If ClearSheet Then
.Worksheet.Cells.Clear
Else
nbRow = .CurrentRegion.Rows.Count: nbRow = nbRow + Abs((nbRow > 1))
If nbRow > 1 And rngTarget.CurrentRegion.Columns.Count <> rngSource.Columns.Count Then
ErrMsg = ErrMsg & "Feuille [" & shtTarget.Name & "] nombre de colonnes différent de la source"
MsgBox ErrMsg, vbOKOnly, ErrTitle
Set shtTarget = Nothing: Set rngTarget = Nothing: Set wkb = Nothing
Exit Function
End If
ClearSheet = nbRow = 1
Set rngTarget = .Worksheet.Range("A" & nbRow) ' Correction 27/3/13 - ajouté parent
End If
End With
' Exportation
With rngSource: .AdvancedFilter xlFilterCopy, areaCriteria, rngTarget: End With
If Not ClearSheet Then rngTarget.EntireRow.Delete: ' Supprime le titre si upgrade
' Collage des largeurs des colonnes
rngSource.Cells.Copy: shtTarget.Cells.PasteSpecial Paste:=xlPasteColumnWidths ' (27/3/13)
Set shtTarget = Nothing: Set rngTarget = Nothing: Set wkb = Nothing: Set rngSource = Nothing
ExportToSheet = True
End Function |
Partager