IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Renommer un tableau reference en Vba


Sujet :

Macros et VBA Excel

  1. #1
    Membre du Club
    Homme Profil pro
    Ingenieur Securite
    Inscrit en
    Mai 2012
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingenieur Securite
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 97
    Points : 61
    Points
    61
    Par défaut Renommer un tableau reference en Vba
    Bonjour a toutes et tous.

    Mon projet consiste a dupliquer un onglet "--Template--" contenant un tableau reference "Template" via un userform.
    Mon probleme se situe lorsque je souhaite renommer le tableau dans l'onglet copie avec la valueur de mon Textbox.

    J'ai prevu une action de remplacement des espaces par "_".
    Ci dessous la partie du code en question.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    'Add a new tab
        Sheets("--Template--").Select 'Sélection de l'onglet "Template"
        Sheets("--Template--").Copy After:=Sheets(5)
        Sheets("--Template-- (2)").Select
        Sheets("--Template-- (2)").Name = TxtBox_Training.Value
        Range("B1") = TxtBox_Training.Value
        Range("B1").Replace What:=" ", Replacement:="_", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        ActiveSheet.ListObjects("").Name = Range("B1").Value
    Et la, l'ensemble du code pour vous donner une idee globale.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    Private Sub UserForm_Initialize()
    End Sub
    Private Sub CdB_Ok_Click()
    Dim nb_column1 As Long, nb_column2 As Long, New_WS As String
        nb_column1 = Worksheets("Employees List").UsedRange.Columns.Count
        nb_column2 = Worksheets("Training Hours").UsedRange.Columns.Count
     
        Application.ScreenUpdating = False
     
    'Activate "Training List" sheet
    With Worksheets("Training List").Activate
        ActiveSheet.Unprotect
     
        'Insert row under copied row
            Rows("10:12").Select
            Selection.EntireRow.Hidden = False
            Rows("11:11").Select
            Selection.Copy
            Rows("12:12").Select
            Selection.Insert Shift:=xlDown
            Range("B12").Select
            Application.CutCopyMode = False
            ActiveCell.FormulaR1C1 = TxtBox_Training.Value
    End With
     
    'Add a new tab
        Sheets("--Template--").Select 'Sélection de l'onglet "Template"
        Sheets("--Template--").Copy After:=Sheets(5)
        Sheets("--Template-- (2)").Select
        Sheets("--Template-- (2)").Name = TxtBox_Training.Value
        Range("B1") = TxtBox_Training.Value
        Range("B1").Replace What:=" ", Replacement:="_", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        ActiveSheet.ListObjects("").Name = Range("B1").Value
     
    'Activate "Training List" sheet
    With Worksheets("Training List").Activate
    New_WS = TxtBox_Training.Value
     
        'Hide reference row
            Rows("11:11").Select
            Selection.EntireRow.Hidden = True
     
        'Replace tab address in formulas
            Range("G12:I12").Select
            Selection.Replace What:="--Template--", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
     
        'Replace table's name in formulas
            Range("H12").Select
            Selection.Replace What:="Template", Replacement:=Worksheets(New_WS).Range("B1").Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
     
        'Add hypertext link of new tab
            Range("B12").Select
            xxx = TxtBox_Training.Value
            ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
               xxx & "!Print_Area", TextToDisplay:=xxx
     
        'Sort training list A to Z
            ActiveWorkbook.Worksheets("Training List").AutoFilter.Sort.SortFields. _
                Clear
            ActiveWorkbook.Worksheets("Training List").AutoFilter.Sort.SortFields. _
                Add Key:=Range("B10"), SortOn:=xlSortOnValues, Order:=xlAscending, _
                DataOption:=xlSortNormal
            With ActiveWorkbook.Worksheets("Training List").AutoFilter.Sort
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
     
        'Protect sheet
        ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:= _
            False, AllowSorting:=True, AllowFiltering:=True
     
    End With
     
    'Activate "Employees List" sheet
    With Worksheets("Employees List").Activate
        ActiveSheet.Unprotect
     
        'Unhide hidden column
            Columns("G:K").Select
            Selection.EntireColumn.Hidden = False
     
        'Copy of reference column
            Columns("H:J").Select
            Selection.Copy
     
        'Insert at the end of table a new column
            Columns(nb_column1 + 1).Select
            Selection.Insert Shift:=xlToRight
            Application.CutCopyMode = False
     
        'Replace "--Template--" by the tab's name
            Columns(nb_column1 + 1).Select
            Selection.Replace What:="--Template--2", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            Selection.Replace What:="--Template--", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
     
            Columns(nb_column1 + 2).Select
            Selection.Replace What:="--Template--", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            Selection.Replace What:="Year3", Replacement:="Year", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            Selection.EntireColumn.Hidden = True
     
            Columns(nb_column1 + 3).Select
            Selection.Replace What:="--Template--", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            Selection.Replace What:="Hours4", Replacement:="Hours", LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
            Selection.EntireColumn.Hidden = True
     
        'Hide reference column
            Columns("H:J").Select
            Selection.EntireColumn.Hidden = True
     
        'Protect sheet
            ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
                , AllowSorting:=True, AllowFiltering:= _
                True
     
    End With
     
    'Activate "Training Hours" sheet
    With Worksheets("Training Hours").Activate
     
        'Unhide hidden column
            Columns("E:G").Select
            Selection.EntireColumn.Hidden = False
     
        'Copy of reference column
            Columns("F:F").Select
            Selection.Copy
     
        'Insert at the end of table a new column
            Columns(nb_column2 + 1).Select
            Selection.Insert Shift:=xlToRight
            Application.CutCopyMode = False
     
        'Replace "--Template--" by the tab's name
            Selection.Replace What:="--Template--", Replacement:=TxtBox_Training.Value, LookAt:= _
                xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                ReplaceFormat:=False
     
        'Hide reference column
            Columns("F:F").Select
            Selection.EntireColumn.Hidden = True
     
    End With
     
    'Close userform
        Unload Me
        Worksheets("Training List").Select
     
    End Sub
    Private Sub CdB_Cancel_Click()
        Unload Me
    End Sub
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        If CloseMode = vbFormControlMenu Then Cancel = True
    End Sub
    Par la meme occasion au niveau des lignes 51 a 55 je souhaite changer le nom du tableau reference dans une formule.

    Merci par avance pour votre collaboration.

    Cordialement

    Laurent

  2. #2
    Membre éprouvé
    Homme Profil pro
    Directeur
    Inscrit en
    Avril 2003
    Messages
    724
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Avril 2003
    Messages : 724
    Points : 1 166
    Points
    1 166
    Par défaut
    Salut,

    ok, mais quelle est ta question?
    Doit on debuger ton code?
    Dans ce cas, autant fournir un classeur exemple!
    Cordialement,

  3. #3
    Membre du Club
    Homme Profil pro
    Ingenieur Securite
    Inscrit en
    Mai 2012
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingenieur Securite
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 97
    Points : 61
    Points
    61
    Par défaut
    Mon probleme est que je ne sais pas comment comment coder le changement de nom du tableau reference, sachant qu'a chaque fois que je copie l'onglet "--Template--" le nom du tableau change (Ex: Template38)

    Cordialement

  4. #4
    Membre éprouvé
    Homme Profil pro
    Directeur
    Inscrit en
    Avril 2003
    Messages
    724
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Directeur

    Informations forums :
    Inscription : Avril 2003
    Messages : 724
    Points : 1 166
    Points
    1 166
    Par défaut
    Ah! ok!
    et ton tableau tu obtiens sa référence avec ListObjets("blablbla")?
    Mais si il n'y a qu'un seul tableau sur la nouvelle feuille, tu peux y accéder
    en passant un index numérique, comme;
    ListObjects(0) ou ListObjects(1)
    Tu n'as pas besoin de connaitre son nom!

  5. #5
    Membre du Club
    Homme Profil pro
    Ingenieur Securite
    Inscrit en
    Mai 2012
    Messages
    97
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Tarn (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingenieur Securite
    Secteur : Industrie

    Informations forums :
    Inscription : Mai 2012
    Messages : 97
    Points : 61
    Points
    61
    Par défaut
    Merci Philippe.

    ListObjects(1) marche.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. c# tableau reference
    Par zoronoa dans le forum C#
    Réponses: 4
    Dernier message: 22/06/2008, 21h05
  2. comment retourner un tableau de double à VBA
    Par geminium dans le forum Général VBA
    Réponses: 2
    Dernier message: 01/06/2007, 19h11
  3. [VBA-E] Construction d'un tableau EXCEL avec VBA
    Par oliver75 dans le forum Macros et VBA Excel
    Réponses: 7
    Dernier message: 02/05/2007, 07h35
  4. [VBA-E] Problème de tableau dans macro VBA
    Par Chouls dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 12/06/2006, 15h20
  5. [VBA-E]créer un tableau Excel en vba
    Par DonKnacki dans le forum Macros et VBA Excel
    Réponses: 9
    Dernier message: 19/01/2006, 16h06

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo