ListBox Compile Error

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I dont normaly use Listbox but trying it today.

I can load listbox fine but i get a compile error when i select an item to the n delete a record.
The code below in red is my issue but not sure why & also assume once that is sorted the rest is ok
Rich (BB code):
Private Sub ListBox1_Click()
For i = Range "GRASS!A5:B" & Range("B" & Rows.Count).End(xlUp).Row
   If listDisplay.Selected(i) Then
        Rows(i).Delete
    End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
It's hard to understand what you want to do just by putting the error.

You must explain what you need to do.
  • Do you want to delete a record in the sheet?
  • Do you want to delete the record in the listbox?
  • Is the listbox in the sheet or in a userform?
  • How did you load the data into the listbox?
You see?
There are more doubts than solutions to your problem.

I'm going on an adventure with this:
If you are going to
- delete the listbox record,
- in a userform,
- with the data loaded into the listbox with the Additem method,
- when you select it with a click, then try this:


VBA Code:
'This is a global variable declaration, it must go at the beginning of all code.
Dim deleting As Boolean

Private Sub ListBox1_Click()
  If deleting = True Then Exit Sub
  deleting = True
  With ListBox1
    .RemoveItem (.ListIndex)
  End With
  deleting = False
End Sub
 
Upvote 0
Hi,Best i show what i have at present.

The information from the worksheet is loaded intop the userform listbox.
The user makes a selection in the listbox example "TOM JONES"
The record on the worksheet for TOM JONES should now be deleted.



Rich (BB code):
Option Explicit
Private Sub CommandButton1_Click()
Unload UserForm1
End Sub

Private Sub ListBox1_Click()
For i = Range "GRASS!A5:B" & Range("B" & Rows.Count).End(xlUp).Row
   If listDisplay.Selected(i) Then
        Rows(i).Delete
    End If
End Sub

Private Sub UserForm_Initialize()
ListBox1.RowSource = "GRASS!A5:B" & Range("B" & Rows.Count).End(xlUp).Row
End Sub
 
Upvote 0
Yes delete the record from the worksheet.
Yes delete the record from the listbox.
The listbox is on a userform.
 
Upvote 0
To delete a record by clicking, the following is enough, the detail is that the following is selected.

VBA Code:
Option Explicit

Dim deleting As Boolean

Private Sub ListBox1_Click()
  If deleting = True Then Exit Sub
  deleting = True
  Sheets("GRASS").Rows(ListBox1.ListIndex + 5).Delete
  deleting = False
End Sub

Private Sub UserForm_Initialize()
  Dim lr As Long
  lr = Sheets("GRASS").Range("B" & Rows.Count).End(xlUp).Row
  If lr > 4 Then ListBox1.RowSource = "GRASS!A5:B" & lr
End Sub

Private Sub CommandButton1_Click()
  Unload UserForm1
End Sub

I recommend that you use the double click event, in this way the next item will not be selected. So to delete an item you must press double click.
VBA Code:
Option Explicit

Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
  Dim i As Long
  Dim lr As Long
  Dim sh As Worksheet

  Set sh = Sheets("GRASS")
  With ListBox1
    sh.Rows(.ListIndex + 5).Delete
    lr = sh.Range("B" & Rows.Count).End(xlUp).Row
    If lr > 4 Then .RowSource = sh.Name & "!A5:B" & lr
    For i = 0 To .ListCount - 1
      If .Selected(i) Then .Selected(i) = False
    Next
  End With
End Sub

Private Sub UserForm_Initialize()
  Dim lr As Long
  lr = Sheets("GRASS").Range("B" & Rows.Count).End(xlUp).Row
  If lr > 4 Then ListBox1.RowSource = "GRASS!A5:B" & lr
End Sub

Private Sub CommandButton1_Click()
  Unload UserForm1
End Sub
Try the 2 codes and see which one solves what you need.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,618
Messages
6,120,544
Members
448,970
Latest member
kennimack

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