Tweak Delete Row VBA code to ask if the user is sure.

bsnow

New Member
Joined
Mar 29, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi guys

I need to tweak the code I have to delete a row in a table to display a dialogue box with a message asking if the user is sure they want to delete the row.

'VBA Delete the First Row from the Table
Sub Delete_First_Row_from_Table()

'Declare Variables
Dim oSheetName As Worksheet
Dim sTableName As String
Dim loTable As ListObject

'Define Variable
sTableName = "Table1"

'Define WorkSheet object
Set oSheetName = Sheets("Main")

'Define Table Object
Set loTable = oSheetName.ListObjects(sTableName)

If loTable.ListRows.Count > 1 Then
'Delete a Row from the table
loTable.ListRows(1).Delete
End If
End Sub


I'd like it so when the button is pressed, a message appears "Are you sure you want to delete the last batch? and offer a yes or no option. If 'yes' is selected, the row will be deleted, if 'no' is selected, the row won't delete. It will hopefully reduce the number of users clicking delete by mistake!

Thanks guys!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi,
try

VBA Code:
'VBA Delete the First Row from the Table
Sub Delete_First_Row_from_Table()
    
    'Declare Variables
    Dim oSheetName  As Worksheet
    Dim sTableName  As String
    Dim loTable     As ListObject
    Dim Response    As VbMsgBoxResult
    
    'Define Variable
    sTableName = "Table1"
    
    'Define WorkSheet object
    Set oSheetName = Worksheets("Main")
    
    'Define Table Object
    Set loTable = oSheetName.ListObjects(sTableName)
    
    If loTable.ListRows.Count > 1 Then
        Response = MsgBox("Are you sure you want To delete the last batch?", 292, "Delete Row")
        If Response = vbNo Then Exit Sub
        'Delete a Row from the table
        loTable.ListRows(1).Delete
    End If
End Sub

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,940
Messages
6,122,356
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