Auto delete button located on top of specific cell

Biker57

New Member
Joined
May 11, 2016
Messages
2
Sir, Ma'am,
I have a piece of code that locks a cell down after the user selects items from the drop-down list associated with the cell. There is a button on the adjacent cell to the right that needs to be automatically deleted when several items of the drop-down list are chosen. The drop down list choices are " ", "As required", "Monthly", "Quarterly", and "Annual". Selecting ". When Blank, Monthly, Quarterly and Annual are chosen the button on cell to right needs to be removed or deleted. The code below locks down the cell but does not delete the button as required.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MyRange As Range
    Dim bu As Button
    
    Set MyRange = Intersect(Range("C5:C133"), Target)
    If Not MyRange Is Nothing Then
        ActiveSheet.Unprotect Password:="CLS"
        MyRange.Locked = True
        If MyRange = "" Or MyRange = "Monthly" Or MyRange = "Quarterly" Or MyRange = "Annual" Then
            For Each bu In ActiveSheet.Buttons
                If bu.TopLeftCell.Address = ActiveCell.Offset(0, 1).Address Then bu.Delete
            Next
        End If
        ActiveSheet.Protect Password:="CLS"
    End If
End Sub

[/End Code]

Thank you in advance for any help
Biker57
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
At first blush, I don't see any specific reason the code above wouldn't work.

Are you sure the objects are actual buttons and not simply shapes?

Perhaps try replacing .Buttons with .Shapes

e.g. For Each bu in ActiveSheet.Shapes
 
Upvote 0
Mr. BiocideJ,
Thank you, you were correct the buttons are not buttons they are shapes. It revealed a second issue though. The button is removed automatically but not before a Run-time error '1004': 'Application-defined or object-defined error' which is on the following line,

If bu.TopLeftCell.Address = ActiveCell.Offset(0, 1).Address Then bu.Delete.

In debug the line is highlighted yellow up to the word Then. When I hit end on the error window the button disappears and the target cell is locked down as hoped for.

Regards,
Biker57
 
Upvote 0
I recreated your scenario and it 'kind of' works the way I would expect. I wasn't using drop-downs and when I press enter to confirm the cell value, it was changing my ActiveCell location and was then deleting a the shape in a row below where I expected.

I suspect with drop-downs the ActiveCell *should* always be the same as the MyRange cell, but I think the following code should work (at least it does for me) the way you are requesting.

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim MyRange As Range
    Dim bu As Shape

    Set MyRange = Intersect(Range("C5:C133"), Target)
    If Not MyRange Is Nothing Then
        ActiveSheet.Unprotect Password:="CLS"
        MyRange.Locked = True
        If MyRange = "" Or MyRange = "Monthly" Or MyRange = "Quarterly" Or MyRange = "Annual" Then
            For Each bu In ActiveSheet.Shapes
                If bu.TopLeftCell.Address = MyRange.Offset(0, 1).Address Then bu.Delete
            Next
        End If
        ActiveSheet.Protect Password:="CLS"
    End If
End Sub

I've highlighted my changes with red text so you can see what I've changed.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
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