par , 18/01/2020 à 08h32 (743 Affichages)
Dans mon précédent billet, j'ai présenté une fonction IsListObjectExist qui renvoyait True ou False si un tableau structuré était présent ou pas dans le classeur
Voici une autre approche avec la fonction nommée GetListOb jectSheet qui renvoie l'objet Worksheet où se trouve l'objet ListObject ou Nothing si celui n'est pas présent dans le classeur définit par le second argument (optionnel) qui est par défaut le classeur actif
Code de la procédure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| Function GetListObjectSheet(ListName As String, Optional oWkb As Workbook) As Worksheet
' Philippe Tulliez www.magicoffice.be
' Fonction renvoyant l'objet feuille où se trouve le tableau. Nothing s'il n'existe pas
' Arguments
' ListName Nom du tableau
' [oWkb] Objet Workbook où la recherche doit avoir lieu (ActiveWorkbook si omis)
' Déclaration
Dim cSht As Integer
' Assignation
If oWkb Is Nothing Then Set oWkb = ActiveWorkbook
'
Do
cSht = cSht + 1
On Error Resume Next
Set GetListObjectSheet = oWkb.Worksheets(cSht).ListObjects(ListName).Parent
On Error GoTo 0
Loop While cSht < oWkb.Worksheets.Count And GetListObjectSheet Is Nothing
End Function |
Code d'une procédure qui invoque cette fonction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| Sub TestGetListObjectSheet()
' Déclaration
Const TableName As String = "T_pd"
Dim oLstSheet As Worksheet
Dim msg As String
' Assignation
Set oLstSheet = GetListObjectSheet(TableName, Workbooks(1))
msg = "Le tableau nommé ["
'
If oLstSheet Is Nothing Then
msg = msg & TableName & "] n'existe pas"
Else
msg = msg & TableName & "] se trouve dans la feuille nommée [" & oLstSheet.Name & "]"
End If
MsgBox msg
End Sub |