Deleting Pictures From In Merged Cell

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all...
i have macro code to delete all picture (3 pict) in a sheet in condition MERGED cell..
the picture in cell B6 (no merge cell), C6:D6 (merged cell) and E6:F6 (merged cell)
here this code :
VBA Code:
Sub deletePicturesFromMergedCells()
    Dim sh As Shape, ws As Worksheet
    Dim rng As Range

    Set ws = Worksheets......( [B]" i want name of sheet is random)"[/B])

    For Each sh In ws.Shapes
        'if shape is picture
        If sh.Type = msoPicture Or sh.Type = msoLinkedPicture Then
            'get entire range where picture placed
            Set rng = ws.Range(sh.TopLeftCell, sh.BottomRightCell)
            'if picture is in range A8 (with megred cells) then delete it
            If Not Intersect(rng, ws.Range("B6:F6").MergeArea) Is Nothing Then
                sh.Delete
            End If
        End If
    Next sh
End Sub

note :
name of sheet are random name
this macro not work for merged cell...
how to modify that code so work in merged cell

thank for your helping
.sst
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Assuming your merged area is B6:F6, maybe something like this:
VBA Code:
Sub DeletePicFromMergedCell()
Dim Pic As Picture, rng As Range
For Each Pic In ActiveSheet.Pictures
    Set rng = Range(Pic.TopLeftCell.Address, Pic.BottomRightCell.Address)
    If Not Intersect(rng, Range("B6:F6")) Is Nothing Then
        Pic.Delete
    End If
Next Pic
End Sub
 
Upvote 0
Or if it is just the top left cell:
Code:
Sub Or_Maybe()
Dim pic As Picture
    For Each pic In ActiveSheet.Pictures
        If pic.TopLeftCell.MergeCells Then pic.Delete
    Next pic
End Sub
 
Upvote 0
Or if it is just the top left cell:
Code:
Sub Or_Maybe()
Dim pic As Picture
    For Each pic In ActiveSheet.Pictures
        If pic.TopLeftCell.MergeCells Then pic.Delete
    Next pic
End Sub
hi Jolivanse...your code not fully work cause 1 picture in cell B6 can't delete..
 
Upvote 0
Assuming your merged area is B6:F6, maybe something like this:
VBA Code:
Sub DeletePicFromMergedCell()
Dim Pic As Picture, rng As Range
For Each Pic In ActiveSheet.Pictures
    Set rng = Range(Pic.TopLeftCell.Address, Pic.BottomRightCell.Address)
    If Not Intersect(rng, Range("B6:F6")) Is Nothing Then
        Pic.Delete
    End If
Next Pic
End Sub
hi JoeMo, thanks working well
 
Upvote 0

Forum statistics

Threads
1,214,960
Messages
6,122,479
Members
449,088
Latest member
Melvetica

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top