Option Explicit
Sub CustomExport()
' Part One: Filter and export
Dim shtGen As Worksheet, shtTotal As Worksheet
Dim cDir, cType
Dim lr As Long
Dim lrt As Long
Set shtGen = ActiveWorkbook.Worksheets("Tab_Général")
Set shtTotal = ActiveWorkbook.Worksheets("RECAP_TOTAL")
' shtGen show all
With shtGen.ListObjects("Tableau12")
.Range.AutoFilter Field:=3
.Range.AutoFilter Field:=14
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("Tableau12[[#All],[Date création]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
' Clear former export
If shtGen.AutoFilterMode Then shtGen.AutoFilterMode = False
lr = shtGen.Range("C" & Rows.Count).End(3).Row
With shtTotal.Range("A16:P" & Rows.Count)
.UnMerge
.ClearContents
.ClearFormats
End With
cDir = shtTotal.Range("B6").Value
cType = shtTotal.Range("B7").Value
With shtGen.Range("A15:P" & lr)
If cDir <> "" Then .AutoFilter 3, cDir Else .AutoFilter 3, "*"
If cType <> "" Then .AutoFilter 14, cType Else .AutoFilter 14, "*"
End With
If shtGen.Range("C" & Rows.Count).End(3).Row > 16 Then
shtGen.AutoFilter.Range.Offset(1).Copy shtTotal.Range("A16")
If shtGen.AutoFilterMode Then shtGen.AutoFilterMode = False
End If
' shtGen show all + chronological order
With shtGen.ListObjects("Tableau12")
.Range.AutoFilter Field:=3
.Range.AutoFilter Field:=14
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("Tableau12[[#All],[Date création]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
End With
With shtGen.ListObjects("Tableau12").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' Msg if no result
lrt = shtTotal.Range("A" & Rows.Count - 1).End(xlUp).Row
With shtTotal.Range("A16:P" & lrt)
If Application.WorksheetFunction _
.Sum(shtTotal.Range("A16:P" & lrt)) = 0 Then
MsgBox "Aucun résultat trouvé"
Else
End If
End With
' =================
' Part two: Offset & Format
Dim rngT As Range
Dim rowT As Long
Dim iP As Range
Dim rPair As Long
Set rngT = shtTotal.Range("A16:P" & lrt)
' Format cells
shtTotal.Range("A16:P" & lrt + 1).RowHeight = 12.75
With shtTotal.Range("H16:K" & lrt + 1)
.Borders(xlEdgeTop).LineStyle = xlContinuous
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeTop).Weight = xlThin
.Borders(xlEdgeLeft).Weight = xlThin
.Borders(xlEdgeRight).Weight = xlThin
.Borders(xlEdgeBottom).Weight = xlThin
End With
' Add blank row X+1
For rowT = rngT.Rows.Count To 2 Step -1
With rngT.Rows(rowT)
.EntireRow.Insert
rngT.Rows(rowT).Columns("B:G").Merge
rngT.Rows(rowT).Columns("B:G").Font.Bold = True
rngT.Rows(rowT).RowHeight = 20
.VerticalAlignment = xlCenter
End With
Next rowT
' Merge blank row below last row
With shtTotal.Range("A" & Rows.Count).End(xlUp).Offset(1) _
.Columns("B:G")
.Merge
.VerticalAlignment = xlCenter
.RowHeight = 20
.Font.Bold = True
End With
' Put P to next row
For Each iP In shtTotal.Range("P16:P300")
If iP.Value <> "" Then
iP.Offset(1, -14).Value = iP.Value
End If
Next iP
' No workerino ;:(
' For Each iP In shtTotal.Range("A16:A" & lrt)
' If IsEmpty(iP.Value) = False Then
' shtTotal.Range(Rows(iP), Rows(iP.Offset(1, 0))).Merge
' End If
' Next iP
shtTotal.Columns("M").Delete
shtTotal.Columns("O:P").Delete
shtGen.Range("N15").Copy shtTotal.Range("M15")
For rPair = rngT.Rows.Count + 1 To 2 Step -2
With rngT.Rows(rPair).Columns("A:M")
.Borders(xlEdgeLeft).LineStyle = xlContinuous
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
End With
Next rPair
For rPair = rngT.Rows.Count + 1 To 2 Step -4
With rngT.Rows(rPair).Columns("A:M").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
With rngT.Rows(rPair - 1).Columns("A:M").Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorDark1
.TintAndShade = -0.149998474074526
.PatternTintAndShade = 0
End With
Next rPair
With rngT.Range("M15:M" & lrt + 1)
.Borders(xlEdgeRight).LineStyle = xlContinuous
.Borders(xlEdgeRight).Weight = xlMedium
End With
End Sub