error on deleting last row

spanner28

New Member
Joined
Jul 23, 2024
Messages
4
Office Version
  1. 2013
Platform
  1. Windows
Hi, i always encounter error whenver i delete last entry on the excel using the selected indx on listbox

i have a listbox SBL_list.List with items coming form Worksheet SBL_T2K ( 9 columns)

i need to delete the selected entire row on the listbox from the excel sheet.
no error encountered when deleting other rows beside the last row

VBA Code:
        Worksheets("SBL_T2K").Select
    For j = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
      If Cells(j, 1) = SBL_list.List(SBL_list.ListIndex) Then
        Rows(j).Delete
      End If
    Next j

 
Last edited by a moderator:

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
And which type of error do you get? On which of the listed vba instruction?
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0
When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
Thanks for this!
 
Upvote 0
And which type of error do you get? On which of the listed vba instruction?
I am having runtime error 381: could not get the list property. invalid property array index on line

VBA Code:
If Cells(j, 1) = SBL_list.List(SBL_list.ListIndex) Then
 
Upvote 0
That is the error that occurs when no item is selected in the listbox...

Try embedding your For j /Next j loop in an If as follows:
VBA Code:
If SBL_list.ListIndex >= 0 Then
    For j = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
      If Cells(j, 1) = SBL_list.List(SBL_list.ListIndex) Then
        Rows(j).Delete
      End If
    Next j
End If

Beware: I am not sure that the line Worksheets("SBL_T2K").Select will really select that sheet, if the userform is open as vbModal (the standard mode); it would be better to fully address the worksheet in the relevant instructions, rather than assuming it is the active one. For more specific suggestions we need the code of the full macro
 
Upvote 0

Forum statistics

Threads
1,221,181
Messages
6,158,395
Members
451,489
Latest member
tixarah

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