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
| Option Explicit
Option Base 1 'n'oublies pas ça
Sub Bloc()
Dim x As Integer, w As Integer, DerniereLigne As Integer, dernierecolonne As Integer
Dim z As Integer, deb() As Integer, fin() As Integer, y As Integer
Dim separ As String, f As String
'***************************************************************
DerniereLigne = ActiveSheet.Range("D" & Rows.Count).End(xlUp).Row
dernierecolonne = ActiveSheet.Range("IV2").End(xlToLeft).Column
'/////////////////////////////////////////////////////////////////////////////////
'Pour 1er entete
ActiveWorkbook.Names.Add Name:="Sheet1Head1", RefersToR1C1:="=sheet1!R1C1:R2C3"
'pour 2eme entete
ActiveWorkbook.Names.Add Name:="Sheet1Head2", RefersToR1C1:="=sheet1!R1C5:R2C24"
'pour suivants
w = 3
For x = 25 To dernierecolonne + 20
If dernierecolonne < x + 19 Then
ActiveWorkbook.Names.Add Name:="Sheet1Head" & w, RefersToR1C1:="=sheet1!R1C" & x & ":R2C" & dernierecolonne & ""
Exit For
Else
ActiveWorkbook.Names.Add Name:="Sheet1Head" & w, RefersToR1C1:="=sheet1!R1C" & x & ":R2C" & x + 19 & ""
End If
w = w + 1
x = (20 * (w - 2)) + 4
Next x
'//////////////////////////////////////////////////////////////////////////////////
'alimente les deux tableaux debut et fin des noms à définir
z = 1
ReDim deb(2)
ReDim fin(2)
deb(1) = 3
'***************************************************************
For x = 3 To DerniereLigne
If Range("a" & x).Interior.ColorIndex = 48 Then ' code couleur
deb(z + 1) = Range("a" & x).Row
fin(z) = Range("a" & x - 1).Row
z = z + 1
ReDim Preserve deb(z + 1)
ReDim Preserve fin(z + 1)
End If
Next x
'*******************************************************************
fin(z) = DerniereLigne
'*************************************************************************
'pour le premier bloc car tu en auras 1 d'office
For x = 1 To UBound(deb, 1) - 1
separ = Application.WorksheetFunction.Substitute(Range("a" & deb(x)).Value, "-", "_")
f = "" & "=sheet1!R" & deb(x) & "C1:R" & fin(x) & "C3" & ""
ActiveWorkbook.Names.Add Name:="sheet1" & separ, RefersToR1C1:=f
Next x
'******************************************************************
'pour le deuxième bloc car tu en auras 1 d'office
For x = 1 To UBound(deb, 1) - 1
separ = Application.WorksheetFunction.Substitute(Range("a" & deb(x)).Value, "-", "_")
f = "" & "=sheet1!R" & deb(x) & "C5:R" & fin(x) & "C24" & ""
ActiveWorkbook.Names.Add Name:="sheet" & "2" & separ, RefersToR1C1:=f
Next x
'***************************************************************************
'pour le reste
y = 25
For x = 1 To UBound(deb, 1)
separ = Application.WorksheetFunction.Substitute(Range("a" & deb(x)).Value, "-", "_")
f = "" & "=sheet1!R" & deb(x) & "C" & y & ":R" & fin(x) & "C" & y + 19 & ""
ActiveWorkbook.Names.Add Name:="sheet" & x + 2 & separ, RefersToR1C1:=f
y = y + 20
If y >= dernierecolonne - 19 Then
Exit For
End If
Next x
w = x + 3
'*************************************************************************
'pour la derniere colonne
For x = 1 To UBound(deb, 1) - 1
separ = Application.WorksheetFunction.Substitute(Range("a" & deb(x)).Value, "-", "_")
f = "" & "=sheet1!R" & deb(x) & "C" & y & ":R" & fin(x) & "C" & dernierecolonne & ""
ActiveWorkbook.Names.Add Name:="sheet" & w & separ, RefersToR1C1:=f
Next x
End Sub |