Add delete button in table column ONLY if condition is met.

duteberta

Board Regular
Joined
Jun 14, 2009
Messages
89
Office Version
  1. 365
Platform
  1. MacOS
How do I have a DELETE button appear in the "d" column (Column "data[d]") only if there is a "1" value in the first column? I already have the Delete row macro written and it works fine - I'm just trying to get a better UI.

Here is my test file:
iCloud Drive - Apple iCloud

Screenshot 2024-04-13 at 8.42.54 AM.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Insert in your sheet a commandbutton (from the ControlX toolbox) and assign it the name BUTTON_01

Then in your WorksheetChange routine add the block of instructions outlined below:
VBA Code:
'... more from your code

    Set lo = Me.ListObjects(1)
    lColCnt = lo.ListColumns.Count
    'Added block -->
    Set rngCol1 = Intersect(Target.Cells(1, 1), lo.Range.Columns(1))
    If Not rngCol1 Is Nothing Then
        If rngCol1.Cells(1, 1).Value = 1 Then
            Me.BUTTON_01.Visible = True
        Else
            Me.BUTTON_01.Visible = False
        End If
    End If
    '<-- End Added Block

    Set rngCol1 = Intersect(Target, lo.Range.Columns(4))

    If Not rngCol1 Is Nothing Then
        With Application
 'continue with your code
This should Hide /unhide button_01 when column 1 changes
 
Upvote 0
On the Mac I cannot format buttons (at all). Is there a way I can insert an icon and use that instead? Or even just a Webdings character.
 
Upvote 0
Also trying to figure out where to put this code in my file. Do I put it in my "Delete Row" sub of do is this a separate sub?
 
Upvote 0
The code should be added to your Private Sub Worksheet_Change, inserted in Sheet1 vba class module

Unfortunately I missed that your environment is MacOs, don't know if Visible=True/False is managed there
A text (the webdings option) does not offer a click option.
Also now I realize that probably you would like to have a button in any of the rows with the 1 in column A, whereas my code only manage 1 button

In short, it is very likely that my response was inappropriate, sorry
 
Upvote 0
Here is my sheet code. How do I enter the code to SHOW a delete button ONLY if there is a "1" in Column "1"?

VBA Code:
Option Explicit


'ADD RECORD

Sub AddRecordToTable()
    
    Dim ws As Worksheet
    Dim newRow As ListRow
    
    Set ws = ActiveSheet

    With ws.ListObjects("data")
    
            .ListColumns("1").DataBodyRange.ClearContents
    
        Set newRow = .ListRows.Add(1)
        
                newRow.Range(.ListColumns("1").Index) = "1"
                newRow.Range(.ListColumns("Status").Index) = "PENDING"


    End With

End Sub



'SELECT TABLE ROW

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim tbl           As ListObject
    Dim rngCell     As Range
    
    If Target.Cells.Count > 1 Then Exit Sub
    
    On Error Resume Next
    
    Set tbl = Target.Worksheet.ListObjects(1)
    If tbl Is Nothing Then Exit Sub
    If tbl.ListRows.Count = 0 Then Exit Sub
    
    Set rngCell = Application.Intersect(tbl.ListColumns("1").DataBodyRange, Target)
    If rngCell Is Nothing Then Exit Sub
    On Error GoTo 0
    
    tbl.ListColumns("1").DataBodyRange = ""
    rngCell.Value = 1
    
    
    Dim SelectRow As Long

'Must preceed next Sub

Sub Undo_()
    With Application
        .EnableEvents = False
        .Undo
        .EnableEvents = True
    End With
End Sub



'SOLD STATUS CHANGES ALL ROW TO VALUES

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim rngCol1 As Range
    Dim rng2 As Range
    Dim lo As ListObject
    Dim lColCnt As Long
    Dim i As Long
    Dim v As Variant

    Set lo = Me.ListObjects(1)
    lColCnt = lo.ListColumns.Count

    Set rngCol1 = Intersect(Target, lo.Range.Columns(4))

    If Not rngCol1 Is Nothing Then
        With Application
            .EnableEvents = False
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With

        ReDim v(1 To lColCnt)

        For Each rng In rngCol1
            If LCase(rng.Value) = LCase("SOLD") Then
                i = 0
                
                'Added code
                
                  Dim Answer As VbMsgBoxResult
                  
                    Answer = MsgBox("This will clear all formulas in this row", vbYesNo + vbExclamation + vbDefaultButton2, "CONFIRM SOLD?")
                    
                    If Answer = vbNo Then
                        Call Undo_
                        Exit Sub
                    End If

                'Remember the numerical formats of each column
                
                For Each rng2 In lo.ListRows(rng.Row - lo.Range.Row).Range
                    i = i + 1
                    v(i) = rng2.NumberFormat
                Next rng2

                'Paste values
                
                rng.Resize(, lColCnt).Value = rng.Resize(, lColCnt).Value

                'Restore original formats of each column
                
                For i = 1 To lColCnt
                    lo.ListRows(rng.Row - lo.Range.Row).Range.Cells(1).Offset(, i - 1).NumberFormat = v(i)
                Next i
            End If
        Next rng

'SortMASTER

        With Application
            .EnableEvents = True
            .Calculation = xlCalculationAutomatic
        End With
    End If
    
End Sub


''DELETE ROW

Sub DeleteRow()

    Dim question As String
    Dim dataTable As ListObject
    Dim ws As Worksheet
    Dim i As Long
    Dim rowsToDelete As Range
    Set rowsToDelete = Nothing

    ' Assuming you want to work with the active sheet
    Set ws = ActiveSheet

    ' NAME OF TABLE
    Set dataTable = ws.ListObjects("data")

    Application.ScreenUpdating = False
    
         Set lo = Me.ListObjects(1)
        lColCnt = lo.ListColumns.Count
        'Added block -->
        Set rngCol1 = Intersect(Target.Cells(1, 1), lo.Range.Columns(1))
        If Not rngCol1 Is Nothing Then
            If rngCol1.Cells(1, 1).Value = 1 Then
                Me.BUTTON_01.Visible = True
            Else
                Me.BUTTON_01.Visible = False
            End If
        End If
        '<-- End Added Block
    
        Set rngCol1 = Intersect(Target, lo.Range.Columns(4))
    
        If Not rngCol1 Is Nothing Then
            With Application

    ' Loop through each row in the table
    For i = dataTable.ListRows.Count To 1 Step -1 ' Looping backwards to avoid issues with deleting rows
        
        ' Check if the value in column "1" of the current row is 1
        If dataTable.DataBodyRange.Cells(i, dataTable.ListColumns("1").Index).Value = 1 Then
            
            ' Add the entire row to the range to be deleted
            If rowsToDelete Is Nothing Then
                Set rowsToDelete = dataTable.ListRows(i).Range
            
            Else
                Set rowsToDelete = Union(rowsToDelete, dataTable.ListRows(i).Range)
            
            End If
        
        End If

    Next i
    
    ' Prompt the user to confirm deletion
    If Not rowsToDelete Is Nothing Then
        question = "Are you sure?"
        
        If MsgBox(question, vbOKCancel + vbCritical + vbDefaultButton2, "DELETE RECORD") = vbOK Then
            
            ' Delete all rows at once
            rowsToDelete.Delete
        
        End If

    End If

    Application.ScreenUpdating = True

End Sub
 
Upvote 0
My proposal incuded adding one block of code within your existing
The modified code:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    Dim rngCol1 As Range
    Dim rng2 As Range
    Dim lo As ListObject
    Dim lColCnt As Long
    Dim i As Long
    Dim v As Variant

    Set lo = Me.ListObjects(1)
    lColCnt = lo.ListColumns.Count                          '*******


    'Added block -->
    Set rngCol1 = Intersect(Target.Cells(1, 1), lo.Range.Columns(1))
    If Not rngCol1 Is Nothing Then
        If rngCol1.Cells(1, 1).Value = 1 Then
            Me.BUTTON_01.Visible = True
        Else
            Me.BUTTON_01.Visible = False
        End If
    End If
    '<-- End Added Block
    
    
    Set rngCol1 = Intersect(Target, lo.Range.Columns(4))    '*******
    If Not rngCol1 Is Nothing Then
   'Your code continues
You will see the "Added block", inserted between the two lines marked *******

But this will Hide /Unhide a single button, whereas (probably) you was asking for many buttons (this is not clear to me). If one button is sufficient, then the code can easily be enhanced to move the button near the "1"
 
Upvote 0
If one button is sufficient, then the code can easily be enhanced to move the button near the "1"
I mean by adding two lines to set Top and Left of the button:
VBA Code:
        If rngCol1.Cells(1, 1).Value = 1 Then
            Me.BUTTON_01.Visible = True
            Me.BUTTON_01.Top = rngCol1.Cells(1, 1).Top          '++ TOP
            Me.BUTTON_01.Left = rngCol1.Cells(1, 2).Left        '++ LEFT
        Else
maarked ++ TOP and ++ LEFT above
 
Upvote 0
It's not working for me. Can I use an icon instead of a button? Or show text "X" that I can click to run DELETE ROW macro?
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,109
Members
452,302
Latest member
TaMere

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