Best Way To Delete All Shapes And Adjacent Cell?

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
If I have images through B1:B10 and values in A1:A10, what's the best way to delete all images and left value whilst keeping all the values in C onwards to remain in the exact same place?

I only want to delete adjacent cell if an image is there so something like this but this really messes up the sheet lol.

Before macro

A1 Value B1 Image C1 1
A2 Value B2 Blank C2 2
A3 Value B3 Image C3 3
A4 Value B4 Blank C4 4

Finished result

A1 Value B1 Blank C1 1
A2 Value B2 Blank C2 2
A3 Blank B3 Blank C3 3
A4 Blank B4 Blank C4 4

Sub getLocation()

Dim wks As Worksheet

Set wks = Sheets("Sheet1")

For Each sshapes In wks.Shapes

x = sshapes.TopLeftCell.Row

wks.Cells(x, 2).Offset(0, -1).Delete

sshapes.Delete

Next

End Sub
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If I have images through B1:B10 and values in A1:A10, what's the best way to delete all images and left value whilst keeping all the values in C onwards to remain in the exact same place?

I only want to delete adjacent cell if an image is there so something like this but this really messes up the sheet lol.

Before macro

A1 Value B1 Image C1 1
A2 Value B2 Blank C2 2On
A3 Value B3 Image C3 3
A4 Value B4 Blank C4 4

Finished result

A1 Value B1 Blank C1 1
A2 Value B2 Blank C2 2
A3 Blank B3 Blank C3 3
A4 Blank B4 Blank C4 4
Give this macro a try...
Code:
Sub DeleteShapesAndClearPrecedingCell()
  Dim Sh As Shape
  Application.ScreenUpdating = False
  For Each Sh In ActiveSheet.Shapes
    If Sh.TopLeftCell.Column = 2 Then
      Cells(Sh.TopLeftCell.Row, "A").ClearContents
      Sh.Delete
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Since I already did this, mine is similar to Rick's.
Code:
Sub getLocation()
  Dim s As Shape
  
  For Each s In Sheets("Sheet1").Shapes
    If s.TopLeftCell.Column = 2 Then
      s.TopLeftCell.Offset(, -1).ClearContents
      s.Delete
    End If
  Next s
End Sub
 
Upvote 0
Since I already did this, mine is similar to Rick's.
Code:
Sub getLocation()
  Dim s As Shape
  
  For Each s In Sheets("Sheet1").Shapes
    If s.TopLeftCell.Column = 2 Then
      s.TopLeftCell.Offset(, -1).ClearContents
      s.Delete
    End If
  Next s
End Sub

Both work in a sense, however I need the offset.clearcontents to use delete instead as I don't want gaps however when I change to delete, when it then deletes the picture it shifts to and brings the C to B.
 
Upvote 0
Both work in a sense, however I need the offset.clearcontents to use delete instead as I don't want gaps however when I change to delete, when it then deletes the picture it shifts to and brings the C to B.
I don't understand... you have data in other columns (C on up)... if you physically delete the values in Column A, the filled cells will move up and become out of sync with the other data that was originally on the same row. I though you said you did not want that to happen when you said "I only want to delete adjacent cell if an image is there so something like this but this really messes up the sheet lol." in Message #1 . Can you describe, in detail, how the data looks before the macro is run and then how it should look afterwards?
 
Upvote 0
I don't understand... you have data in other columns (C on up)... if you physically delete the values in Column A, the filled cells will move up and become out of sync with the other data that was originally on the same row. I though you said you did not want that to happen when you said "I only want to delete adjacent cell if an image is there so something like this but this really messes up the sheet lol." in Message #1 . Can you describe, in detail, how the data looks before the macro is run and then how it should look afterwards?

Of course, sorry for not clarifying.

Before Macro After Macro
Serial # Image Fixed Serial # Image Fixed
Serial1 1 Serial1 1
Serial2 Picture 2 Serial3 2
Serial3 3 Serial5 3
Serial4 Picture 4 Serial6 4
Serial5 5 Serial8 5
Serial6 6 Serial10 6
Serial7 Picture 7 7
Serial8 8 8
Serial9 Picture 9 9
Serial10 10 10
 
Upvote 0
Wow that is coming out so wrong. It won't format correctly!

So if I have a list of serial #'s in A, serial1.. serial10 and then either images or no images in B.. a list of 1 to 10 in C.

If a cell in B contains an image, delete that image and delete the value left in A and shift ONLY A up. So once ran, C will still have 1 to 10.

A will have a new list, maybe serial1, serial5, serial6 as serial2, 3, 4 will have been deleted with corresponding image. B will be empty. C will still have 1 through to 10.
 
Upvote 0
So if I have a list of serial #'s in A, serial1.. serial10 and then either images or no images in B.. a list of 1 to 10 in C.

If a cell in B contains an image, delete that image and delete the value left in A and shift ONLY A up. So once ran, C will still have 1 to 10.

A will have a new list, maybe serial1, serial5, serial6 as serial2, 3, 4 will have been deleted with corresponding image. B will be empty. C will still have 1 through to 10.
Give this version of my code a try...
Code:
Sub DeleteShapesAndClearPrecedingCell()
  Dim Sh As Shape
  Application.ScreenUpdating = False
  For Each Sh In ActiveSheet.Shapes
    If Sh.TopLeftCell.Column = 2 Then
      Cells(Sh.TopLeftCell.Row, "A").Delete xlShiftUp
      Sh.Delete
    End If
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Give this version of my code a try...
Code:
Sub DeleteShapesAndClearPrecedingCell()
  Dim Sh As Shape
  Application.ScreenUpdating = False
  For Each Sh In ActiveSheet.Shapes
    If Sh.TopLeftCell.Column = 2 Then
      Cells(Sh.TopLeftCell.Row, "A").Delete xlShiftUp
      Sh.Delete
    End If
  Next
  Application.ScreenUpdating = True
End Sub

Close, what I've done is add a list of fruits in A so apple banana pear kiwi etc ALL with corresponding pictures. When run there is still values in A once finished even though they all had pictures.

When stepping through the code it appears to be deleting values and pictures at random(?), would it be better to loop backwards with something like for shp = shp to shp.count step - 1 (not sure on the code for this)
 
Upvote 0
Always test on backup copy.
Code:
Sub Ken2()
  Dim s As Shape, i As Long, j As Long, a
  Dim ws As Worksheet, wb As Workbook, r As Range
  
  Set ws = Sheets("Sheet1")
  For Each s In ws.Shapes
    If s.TopLeftCell.Column = 2 Then i = i + 1
  Next s

  If i = 0 Then Exit Sub
  Application.ScreenUpdating = False
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
  
  ReDim a(1 To i, 1 To 2)
  
  i = 0
  For Each s In Sheets("Sheet1").Shapes
    If s.TopLeftCell.Column = 2 Then
      i = i + 1
      a(i, 1) = s.TopLeftCell.Row
      a(i, 2) = s.Name
    End If
  Next s
  j = i
    
  Set wb = Workbooks.Add
  Set r = Range("A1").Resize(UBound(a, 1), UBound(a, 2))
  r.Value = a
  wb.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With wb.Worksheets("Sheet1").Sort
    .SetRange r
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
  a = r.Value
  wb.Close False
  
  For j = 1 To i
    ws.Shapes(a(j, 2)).Delete
    ws.Cells(a(j, 1), "A").Delete xlUp
  Next j
  
  Application.ScreenUpdating = True
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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