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
|
Sub Assign_Rng_Name()
Dim Wsh As Worksheet
Dim SrcRng As Range, ColRng As Range
Dim RngN As String
Dim ColN As Single, RowOffs As Single
' Init
Set Wsh = ThisWorkbook.Worksheets("Listes sociétés")
Set SrcRng = Wsh.Range("A2").CurrentRegion
Debug.Print SrcRng.Address
If SrcRng.Cells.Count = 0 Then Exit Sub
For ColN = 1 To SrcRng.Columns.Count
Set ColRng = SrcRng.Columns(ColN)
Debug.Print ColRng.Address
If ColN = 1 Then
RngN = CStr(SrcRng(1, ColN).Value)
Set ColRng = ColRng.Offset(1, 0).Resize(ColRng.Rows.Count - 1, 1) ' Toutes les lignes non vides sauf la 1ere
Debug.Print ColRng.Address
ThisWorkbook.Names.Add Name:=RngN, RefersTo:=ColRng
Debug.Print ColRng(1, 1).V
Else:
RngN = Replace(CStr(SrcRng(2, ColN).Value), " ", "_")
If (RngN <> vbNullString) And (Not (IsEmpty(SrcRng(3, ColN)))) Then
Set ColRng = ColRng.Offset(2, 0).Resize(ColRng.Cells.Count - 2, 1) ' Toutes les lignes non vides sauf les 2 premières
Debug.Print ColRng.Address, ColRng.SpecialCells(xlCellTypeConstants).Address
Set ColRng = ColRng.SpecialCells(xlCellTypeConstants)
ThisWorkbook.Names.Add Name:=RngN, RefersTo:=ColRng
End If
End If
Next ColN
End Sub |
Partager