Tester si une plage nommée existe
P
Bonjour,
Je souhaite savoir si mes plages nommées ont été initialisées, en gros si elles existent dans mon classeur. Je vous joint le code que j'ai essayé de faire. L'erreur est qualificateur incorrect, merci.
'Fonction pour vérifier si une lsite nommée existe
Function CheckRanges(champs As String) As Boolean
Dim checkRg As Integer
'On initialise la varibale avec le nom de la liste nommée
checkRg = Len(ThisWorkbook.Names(champs.Name))
If checkRg <> 0 Then
CheckRanges = True
Else
CheckRanges = False
End If
End Function
'----------------------------------Code pour permettre l'utilisation de la molette dans les listes déroulantes
Private Sub ComboBox1_Change()
Dim posteSelect As String
Dim nbrePoste As Integer
Dim i As Integer
Dim listeInfo(7) As Boolean
'On compte le nombre de poste dans liste
nbrePoste = Me.ComboBox1.ListCount
'On récupère le poste choisit
posteSelect = ComboBox1.Value
'On regarde si chaque champs du poste sont initialisé, imputation, typologie, etc..
listeInfo(0) = CheckRanges("ImputationPoste" & posteSelect)
listeInfo(1) = CheckRanges("TypologiePoste" & posteSelect)
listeInfo(2) = CheckRanges("IntExt" & posteSelect)
listeInfo(3) = CheckRanges("ZonesPoste" & posteSelect)
listeInfo(4) = CheckRanges("CadrePoste" & posteSelect)
listeInfo(5) = CheckRanges("LissePoste" & posteSelect)
listeInfo(6) = CheckRanges("CotePoste" & posteSelect)
intTopIndex = Me.ComboBox1.TopIndex
'Si la valeur du programme ne correspond pas a un programme en cours alors msg erreur
If Me.ComboBox1.ListIndex < 0 Then
MsgBox "Veuillez saisir un poste de la liste."
Else
For i = 0 To (nbrePoste - 1)
If posteSelect = Me.ComboBox1.List(i) Then
'On vérifie que les listes nommées ne sont pas vides
'Si elles ne sont pas vides alors on insère les informations
If listeInfo(0) = True Then
'On ajoute les informations correspondantes au poste aux combobox
NouvelleRetouche.ComboBox15.List = Sheets("liste").Range("ImputationPoste" & posteSelect).Value
End If
If listeInfo(1) = True Then
NouvelleRetouche.ComboBox9.List = Sheets("liste").Range("TypologiePoste" & posteSelect).Value
End If
If listeInfo(2) = True Then
NouvelleRetouche.ComboBox10.List = Sheets("liste").Range("IntExt" & posteSelect).Value
End If
If listeInfo(3) = True Then
NouvelleRetouche.ComboBox11.List = Sheets("liste").Range("ZonesPoste" & posteSelect).Value
End If
If listeInfo(4) = True Then
NouvelleRetouche.ComboBox12.List = Sheets("liste").Range("CadrePoste" & posteSelect).Value
End If
If listeInfo(5) = True Then
NouvelleRetouche.ComboBox13.List = Sheets("liste").Range("LissePoste" & posteSelect).Value
End If
If listeInfo(6) = True Then
NouvelleRetouche.ComboBox14.List = Sheets("liste").Range("CotePoste" & posteSelect).Value
End If
End If
Next i
End If
End SubthevPassionné d'Excel
- Messages
- 4'199
- Excel
- 2021 FR 64 bits
- Inscrit
- 13/06/2016
- Emploi
- bénévole associations Goutte d'Or
Bonjour,
ci-dessous code
'Fonction pour vérifier si une liste nommée existe
Function CheckRanges(champs As String) As Boolean
Dim nom As Name
CheckRanges = False
For Each nom In ThisWorkbook.Names
If nom.Name Like "*" & champs Then CheckRanges = True: Exit For
Next nom
End FunctionP
Merci thev pour ton message, cela fonctionne parfaitement !