Find the existence of a button in the worksheet

luolovepi

Board Regular
Joined
Jun 9, 2011
Messages
116
I'm trying to detect whether a button exists in each of the cell in column N. If there exists a button, the button should be in a cell.
And I write the code as below. But strangely, how come it always returns me CellNotCovered a false value?

Code:
Pr.Range("N" & i).Activate
        With ActiveCell
            For Each oneShape In .Parent.Shapes
                 CellNotCovered = CellNotCovered And (Application.Intersect(.Cells, Range(oneShape.TopLeftCell, oneShape.BottomRightCell)) Is Nothing)
            Next oneShape
        End With
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Hi

Try this code to check the cells in Sheet2!N1:N5 that have a button (the button completely inside the cell).

Code:
Sub Test()
Dim ws As Worksheet
Dim r As Range, rC As Range
Dim bt As Button
Dim bExists As Boolean
 
Set ws = Worksheets("Sheet2")
Set r = ws.Range("N1:N5")
 
For Each rC In r
    bExists = False
    For Each bt In ws.Buttons
        If Not Intersect(bt.TopLeftCell, bt.BottomRightCell, rC) Is Nothing Then
            bExists = True
            Exit For
        End If
    Next bt
    MsgBox "Cell " & rC.Address & " has " & IIf(bExists, "a", "no") & " button"
Next rC
End Sub
 
Upvote 0
Hi PGC, thanks for the help. I tested the code but I find all return me with no button.

I find your code is able to detect the form button. But all my buttons seem to be activeX button. Then do you happen to know how to detect the existence of ActiveX buttons?

Best regards,
lolo

Hi

Try this code to check the cells in Sheet2!N1:N5 that have a button (the button completely inside the cell).

Code:
Sub Test()
Dim ws As Worksheet
Dim r As Range, rC As Range
Dim bt As Button
Dim bExists As Boolean
 
Set ws = Worksheets("Sheet2")
Set r = ws.Range("N1:N5")
 
For Each rC In r
    bExists = False
    For Each bt In ws.Buttons
        If Not Intersect(bt.TopLeftCell, bt.BottomRightCell, rC) Is Nothing Then
            bExists = True
            Exit For
        End If
    Next bt
    MsgBox "Cell " & rC.Address & " has " & IIf(bExists, "a", "no") & " button"
Next rC
End Sub
 
Upvote 0
I find your code is able to detect the form button. But all my buttons seem to be activeX button.

Hi Lolo

The buttons seem to be activeX button? Did someone else create them?

If the buttons are activeX controls then try this code to check the cells in Sheet2!N1:N5 that have a button (the button completely inside the cell).

Code:
Sub Test()
Dim ws As Worksheet
Dim r As Range, rC As Range
Dim OLEobj As OLEObject
Dim bExists As Boolean
 
Set ws = Worksheets("Sheet2")
Set r = ws.Range("N1:N5")
 
For Each rC In r
    bExists = False
    For Each OLEobj In ws.OLEObjects
        If TypeOf OLEobj.Object Is MSForms.CommandButton Then
            If Not Intersect(OLEobj.TopLeftCell, OLEobj.BottomRightCell, rC) Is Nothing Then
                bExists = True
                Exit For
            End If
        End If
    Next OLEobj
    MsgBox "Cell " & rC.Address & " has " & IIf(bExists, "a", "no") & " button"
Next rC
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,756
Members
452,940
Latest member
rootytrip

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