Réinitialisation des noms de tableau
Bonjour,
Désolé si ce sujet a déjà été traité, c'est que je n'ai pas dû utiliser les bons mots-clés pour ma recherche.
Je dispose d'un classeur contenant 1 seule feuille. Le but est de récupérer dans cette feuille des données venant de différents fichiers Excel à l'aide d'une macro.
Je commence en nettoyant la feuille des données précédemment récupérées à l'aide de :
Range("A14:AD250").ClearDans la feuille, 7 tableaux sont créés à l'aide de cette formule VBA :
Dim table As ListObject
Range("E15:G15").Select
Selection.WrapText = True
Set table = ActiveSheet.ListObjects.Add
Dim table2 As ListObject
Range("I15:J15").Select
Selection.WrapText = True
Set table2 = ActiveSheet.ListObjects.Addetc.
Or, à chaque fois que je relance ma macro, j'ai remarqué que le premier tableau ne se nommera pas Tableau0 mais Tableau6 (en haut à gauche dans l'onglet Création)
Après différents essais de la macro, je suis rendu à Tableau118 et je souhaitais savoir si cela pouvait poser problème à terme, et si il y a un moyen de remettre le compteur à 0 à chaque lancement de la macro ?
En vous remerciant d'avance
Bonjour,
Il est possible de renommer les tableaux dans le code :
table.name = "nomtableau"Mais, ensuite, il faudra faire attention aux doublons car un nom ne peut être attribué qu'à un seul tableau structuré. En tout cas, il vaut mieux contrôler le nom pour éventuellement pouvoir le réutiliser par la suite.
Est-il possible de voir le code en entier (où il y a création des 7 tableaux) ?
Cdlt,
Sub Synthese()
'OPTIMISATION (ECRAN)
Dim BoEcran As Boolean, BoBarre As Boolean, BoEvent As Boolean, BoSaut As Boolean
Dim iCalcul As Integer
BoEcran = Application.ScreenUpdating
BoBarre = Application.DisplayStatusBar
iCalcul = Application.Calculation
BoEvent = Application.EnableEvents
BoSaut = ActiveSheet.DisplayPageBreaks
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
'OPTIMISATION (PRESSE-PAPIER)
Application.DisplayAlerts = False
'NETTOYAGE AVANT LANCEMENT
'Nettoyage fichier avant récupération données
Range("A14:AD250").Clear 'UTILISER USEDRANGE ROWS +1 POUR FAIRE UN NETTOYAGE DYNAMIQUE''''''''''''''''''''''''''''''''''''''''
'CREATION DES EN-TETES DE TYPE (BIDULE, TRUC etc.)
'Fusion des cellules
Range("L14:O14,R14:T14,U14:V14,W14:X14,Y14:Z14,AB14:AD14").MergeCells = True
'Police en gras en-têtes Bidule, Truc etc.
Range("L14:O14,R14:T14,V14,X14,Z14,AB14:AD14").Font.Bold = True
'Création de l'en-tête Bidule
Range("L14") = "Bidule"
With Range("L14:O14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'Création de l'en-tête Truc
Range("R14") = "Truc"
With Range("R14:T14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'Création de l'en-tête Machin
Range("V14") = "Machin"
With Range("V14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'Création de l'en-tête Chose
Range("X14") = "Chose"
With Range("X14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'Création de l'en-tête Lambda
Range("Z14") = "Lambda"
With Range("Z14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'Création de l'en-tête azerty
Range("AB14") = "Azerty"
With Range("AB14:AD14")
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
End With
'CREATION TABLEAUX
'Création des tableaux
Dim table As ListObject
Range("E15:G15").Select
Selection.WrapText = True
Set table = ActiveSheet.ListObjects.Add
Dim table2 As ListObject
Range("I15:J15").Select
Selection.WrapText = True
Set table2 = ActiveSheet.ListObjects.Add
Dim table3 As ListObject
Range("L15:T15").Select
Selection.WrapText = True
Set table3 = ActiveSheet.ListObjects.Add
Dim table4 As ListObject
Range("V15").Select
Selection.WrapText = True
Set table4 = ActiveSheet.ListObjects.Add
Dim table5 As ListObject
Range("X15").Select
Selection.WrapText = True
Set table5 = ActiveSheet.ListObjects.Add
Dim table6 As ListObject
Range("Z15").Select
Selection.WrapText = True
Set table6 = ActiveSheet.ListObjects.Add
Dim table7 As ListObject
Range("AB15:AD15").Select
Selection.WrapText = True
Set table7 = ActiveSheet.ListObjects.AddIl n'y aura que 7 tableaux dans ma feuille pour information
Bonjour,
Voici un essai d'adaptation du code même si je ne recommande pas la fusion de cellules...
Sub Synthese()
'NETTOYAGE AVANT LANCEMENT
Range("A14:AD250").Clear 'UTILISER USEDRANGE ROWS +1 POUR FAIRE UN NETTOYAGE DYNAMIQUE''''''''''''''''''''''''''''''''''''''''
'CREATION DES EN-TETES DE TYPE (BIDULE, TRUC etc.)
'Fusion des cellules
thead = array("L14:O14", "R14:T14", "U14:V14", "W14:X14", "Y14:Z14", "AB14:AD14") 'ref entetes
tval = array("Bidule", "Truc", "Machin", "Chose", "Lambda", "Azerty") 'valeurs entetes
tref = array("E15:G15", "I15:J15", "L15:T15", "V15", "X15", "Z15", "AB15:AD15") 'ref tableaux
'MISE EN FORME EN TETES
for i = lbound(thead) to ubound(thead)
with activesheet.range(thead(i))
.mergecells = true
.font.bold = true
.mergearea.cells(1).value = tval(i)
.HorizontalAlignment = xlHAlignCenter
.VerticalAlignment = xlVAlignCenter
end with
next i
'CREATION TABLEAUX
for i = lbound(tref) to ubound(tref)
with activesheet.range(tref(i))
.wraptext = true
.listobjects.add(source:=.cells).name = "Tableau" & i + 1
end with
next i
end subCdlt,
Bonjour @3GB , merci pour ton aide ! En effet les cellules fusionnées ne font pas bon ménage, mais je souhaite quand même les faire fusionner, j'utiliserai un MergeCells en fin de script sûrement
Merci et à bientôt
Bonjour embe,
Oui, c'est le moins qu'on puisse dire. Si j'étais toi, je les utiliserais en toute fin de script, après le end sub
Et peut-être qu'il faudra prévoir une ligne entre les en-têtes fusionnées et les tableaux, je sais pas...
Merci pour ce retour en tout cas !
Bonne fin de journée !