Check if checkbox has linked cell or if it's #Ref

Beneindias

Board Regular
Joined
Jun 21, 2022
Messages
97
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have an excel file with table that has checkboxes for each row in column B.

When I delete a row, the checkbox that was there, looses it's linked cell.

So, is there any way that I can check if linked cell has a cell, or if it's an "#Ref" error?

I need to check if LinkedCell is an error, and if it is, delete that checkbox.

I was trying this code, but it doesn't work.

VBA Code:
If IsError(Cbx.LinkedCell) Then
    Cbx.Delete
End If

Also tried this code, but this one gives me an error:

"Run-time error 438:
Object doesn't support this property or method"

VBA Code:
If IsError(Cbx.FormControl.LinkedCell) Then
    Cbx.Delete
End If
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this.
The code will kill all the checkboxes with linkedcell= "#REF!".
Strongly suggest SAVE THE EXCEL FILE BEFORE RUNNING the code.
VBA Code:
Sub Del_CheckBox()
    Dim myShapes As Shapes
    Dim e As Shape
    Dim myLinkedcell As String
    Set myShapes = ActiveSheet.Shapes
    On Error Resume Next
    For Each e In myShapes
        If Not IsError(e.OLEFormat.progID) Then
            If InStr(1, e.OLEFormat.progID, "CheckBox") > 0 Then
                myLinkedcell = e.OLEFormat.Object.LinkedCell
                If myLinkedcell = "#REF!" Then
                    e.Delete
                End If
            End If
        End If
    Next e
End Sub
 
Upvote 0
Try this.
The code will kill all the checkboxes with linkedcell= "#REF!".
Strongly suggest SAVE THE EXCEL FILE BEFORE RUNNING the code.
VBA Code:
Sub Del_CheckBox()
    Dim myShapes As Shapes
    Dim e As Shape
    Dim myLinkedcell As String
    Set myShapes = ActiveSheet.Shapes
    On Error Resume Next
    For Each e In myShapes
        If Not IsError(e.OLEFormat.progID) Then
            If InStr(1, e.OLEFormat.progID, "CheckBox") > 0 Then
                myLinkedcell = e.OLEFormat.Object.LinkedCell
                If myLinkedcell = "#REF!" Then
                    e.Delete
                End If
            End If
        End If
    Next e
End Sub

Thanks for your code.

I didn't try it yet, because I just figured out a solution, I was trying to use the IsError function, but it wasn't working, so, I had to campare if LinkedCell = #REF!, as in your solution and I had to add an ElseIf to the If condition that I previously had, who was checking if checkbox was outside of my table. I was trying to use IsError, because, in my mind, its a better way to do it, than to hardcode the error, but oh well...

VBA Code:
Sub Checkboxes_Deletion()
    Dim lastRow As Long
    Dim Sh As Worksheet
    Dim worksheet1 As String: worksheet1 = "Salarios" 'Salarios
    Dim PagoColumn As String: PagoColumn = "B"
    Dim LastRowColumn As String: LastRowColumn = "l:l" 'Include Entire Column.
    Dim Cbx As CheckBox
     
           Set Sh = ActiveSheet
        
        With Sh
            'Número da última linha da tabela
            lastRow = WorksheetFunction.countA(Range(LastRowColumn))
        End With
    
        'Itera pelas Checkboxes da folha
        For Each Cbx In ActiveSheet.CheckBoxes
            'Checa se o espaço não está ocupado por uma checkbox. "lastRow + 8" para que seja considerada a linha após o fim da tabela
            If Not Intersect(Cbx.TopLeftCell, ActiveSheet.Range(PagoColumn & lastRow + 8)) Is Nothing Then
                'Apaga a Checkbox, se esta estiver na linha após a tabela
                Cbx.Delete
            'No caso de não estar após a tabela, este próximo código checa se a checkbox tem ligação a alguma célula
            '(quando a linha é apagada, a checkbox perde a ligação à célula e fica com erro #REF!)
            ElseIf Cbx.LinkedCell = "#REF!" Then
                Cbx.Delete
            End If
        Next Cbx
End Sub

That being said, I will make a copy of my file and try your code as soon as I have some free time.

Thanks again
 
Upvote 0
Solution

Forum statistics

Threads
1,214,944
Messages
6,122,384
Members
449,080
Latest member
Armadillos

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