XL 2016 Images dans tableau structuré

fanch55

XLDnaute Barbatruc
Bonjour à tous,
Je rencontre une bizarrerie dans Excel : J'ai un tableau structuré (Tableau1) .
Une des colonnes contient des images ( une par ligne ) .
Quand je veux vider ce tableau, toutes les lignes sont "vidées" : contenu des cellules etc..
mais il me reste imperturbablement toujours l'image de la première ligne même si la ligne de tableau n'existe plus ( par code ou manuellement ....)

C'est un peu ch... surtout que je ne comprend pas pourquoi ...

Auriez-vous une astuce ou une raison à celà ?

Pour charger les lignes du tableau et des image, cliquer sur "Load"
Pour effacer les lignes, cliquez sur "Clear" ou sélectionnez les lignes et clic-droit: supprimer les lignes du tableau
 

Pièces jointes

  • Images_dans_tableau.xlsm
    91.8 KB · Affichages: 26
Solution
Supprime toutes les Shapes d'abord...
VB:
Sub Efface()
    Dim Sh As Shape
    Dim Cell As Range
   
    With [tableau1].ListObject
        If Not .DataBodyRange Is Nothing Then
            For Each Cell In .ListColumns("Image").DataBodyRange.Cells
                For Each Sh In [tableau1].Parent.Shapes
                    If Sh.Top = Cell.Top + 4 Then
                        Sh.Delete
                        Exit For
                    End If
                Next Sh
            Next Cell
       
            .DataBodyRange.Delete
        End If
    End With
End Sub

Dudu2

XLDnaute Barbatruc
Bonjour,
Ah, la 1ère ligne des tableaux structurés qui est là sans être là quand le DataBodyRange is Nothing !
Un vrai cauchemar. J'ai passé des heures et des heures juste pour faire une fonction de Resize du DataBodyRange en gérant les comportements improbables d'Excel (avec ou sans Header Row ou Totals Row) sur les tableaux structurés.

En fait tu ne peux pas te débarrasser de l'image comme ça, il faut la supprimer spécifiquement.
VB:
Sub Efface()
    Dim Sh As Shape
   
    With [tableau1].Parent.ListObjects("tableau1")
        If Not .DataBodyRange Is Nothing Then
            For Each Sh In [tableau1].Parent.Shapes
                If Sh.Top = .ListColumns("Image").DataBodyRange.Cells(1).Top + 4 Then
                    Sh.Delete
                    Exit For
                End If
            Next Sh
       
            .DataBodyRange.Delete
        End If
    End With
   
   
    'If Not [tableau1].ListObject.DataBodyRange Is Nothing Then [Tableau1[#Data]].Delete
End Sub
 

Dudu2

XLDnaute Barbatruc
Dans le code ci-dessus on ne .Delete que la Shape de la 1ère ligne, pas toutes.
Les Shapes ne sont pas vues comme contenu du tableau, donc tu n'y coupes pas de les traiter séparément, sauf que dans la suppression effectives des lignes 2 à N, elles disparaissent parce qu'incluses dans les lignes du tableau supprimées. Alors que la ligne 1 n'est pas physiquement supprimée.

D'ailleurs il serait plus logique de les supprimer toutes avant de supprimer les lignes du tableau.
 

Dudu2

XLDnaute Barbatruc
Supprime toutes les Shapes d'abord...
VB:
Sub Efface()
    Dim Sh As Shape
    Dim Cell As Range
   
    With [tableau1].ListObject
        If Not .DataBodyRange Is Nothing Then
            For Each Cell In .ListColumns("Image").DataBodyRange.Cells
                For Each Sh In [tableau1].Parent.Shapes
                    If Sh.Top = Cell.Top + 4 Then
                        Sh.Delete
                        Exit For
                    End If
                Next Sh
            Next Cell
       
            .DataBodyRange.Delete
        End If
    End With
End Sub
 

fanch55

XLDnaute Barbatruc
Bon, j'ai peut-être un code par dépit qui fonctionne :
Attention l'ordre est important l'image.delete avant le tableau.delete
VB:
Sub Efface()
    For Each Image In ActiveSheet.Pictures
        If Not Intersect(Image.TopLeftCell, [Tableau1[Image]]) Is Nothing Then Image.Delete
    Next
    If Not [Tableau1].ListObject.DataBodyRange Is Nothing Then [Tableau1[#Data]].Delete
End Sub
Merci @Dudu2, tu m'as mis sur la voie ... ;)
 

laurent950

XLDnaute Accro
Bonsoir
VB:
Sub Efface()
' 1) #Data
    If Not [Tableau1].ListObject.DataBodyRange Is Nothing Then [Tableau1[#Data]].Delete
' 2) Picture
    Dim xPicRg As Range
    Dim xPic As Picture
    Dim xRg As Range
    Application.ScreenUpdating = False
    Set xRg = Range("Tableau1")
    For Each xPic In ActiveSheet.Pictures
        Set xPicRg = Range(xPic.TopLeftCell.Address & ":" & xPic.BottomRightCell.Address)
        If Not Intersect(xRg, xPicRg) Is Nothing Then xPic.Delete
    Next
    Application.ScreenUpdating = True
End Sub
 

laurent950

XLDnaute Accro
Ca fonctionne dans les deux sens :
A ) deletes les images avant le tableau...
VB:
Sub EffaceBis()
    Dim xPicRg As Range
    Dim xPic As Picture
    Dim xRg As Range
    Application.ScreenUpdating = False
    Set xRg = Range("Tableau1")
' 1) Picture (Supression des images dans le tableau structuré)
    For Each xPic In ActiveSheet.Pictures
        Set xPicRg = Range(xPic.TopLeftCell.Address & ":" & xPic.BottomRightCell.Address)
        If Not Intersect(xRg, xPicRg) Is Nothing Then xPic.Delete
    Next
' 2) Data (Suppression des datas dans le tableau structuré)
    xRg.Delete
    Application.ScreenUpdating = True
End Sub

B ) deletes le tableau avant les images...
VB:
Sub EffaceTer()
    Dim xPicRg As Range
    Dim xPic As Picture
    Dim xRg As Range
    Application.ScreenUpdating = False
    Set xRg = Range("Tableau1")
' 2) Data (Suppression des datas dans le tableau structuré)
    xRg.Delete
' 1) Picture (Supression des images dans le tableau structuré)
    For Each xPic In ActiveSheet.Pictures
        Set xPicRg = Range(xPic.TopLeftCell.Address & ":" & xPic.BottomRightCell.Address)
        If Not Intersect(xRg, xPicRg) Is Nothing Then xPic.Delete
    Next
    Application.ScreenUpdating = True
End Sub
 
Dernière édition:

Discussions similaires

Statistiques des forums

Discussions
311 541
Messages
2 080 541
Membres
101 237
dernier inscrit
jocethom