Help with listBox item delete.

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
I have a listbox.

i add the items like this that are located on my source sheet as shown.

my current code works if i select the first item every time. but any time i pick an item down the list, it adds it, but doesnt delete the selection from the row.
I need help making sure it deletes whatever i select from the source, so my list updates next time i click the macro button.

Code:
Private Sub UserForm_Initialize()

Dim KLastRow As Long, K As Long

    KLastRow = Sheets("Source").Range("F" & Rows.Count).End(xlUp).Row
For K = 1 To KLastRow
        KList.AddItem Sheets("Source").Range("F" & K)
Next K

Private Sub KButton_Click()
ActiveCell.Value = "KR " & KList.Value
Sheets("Source").Cells(KList.ListIndex + 6).Delete xlShiftUp
Unload Me
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
i hope this is clear.

so in column F. say i have

A
B
C
D

and B is selected. I want that cell of B deleted so the list now looks like
A
C
D

like i said, right now, it's only deleting correctly if i choose the first value in my listbox
 
Upvote 0
Only deleting correctly from where?

The listbox or the worksheet?

Are you actually deleting the selected item from the listbox?
 
Upvote 0
yes, Norie, thank you for responding, I've tried using your setup as you can see from previous posts that i found in search.

since im populating my listbox from column F, that's where my listbox items are being pulled from.
So I want it to delete from the "source" sheet that will then correspond to my listbox items.

but like i said, if i select the first item, it does what i want, posts selected value where i want, and deletes cell in "source" and updates listbox.

But when i select say the 2nd value, it inputs it correctly, but doesn't delete(leaving the 2nd value that i actually want to delete)

hope that helps
 
Upvote 0
Sorry but what setup are you referring to?

What do you want to delete and where from?

You need to be careful when populating from ranges and then deleting things.

You might actually be deleting something but not the something you want to delete.
 
Upvote 0
this way of adding items from a range
Code:
Private Sub UserForm_Initialize()
Dim KLastRow As Long, K As Long
KLastRow = Sheets("Source").Range("F" & Rows.Count).End(xlUp).Row
For K = 1 To KLastRow
        KList.AddItem Sheets("Source").Range("F" & K)
Next K
Is there a way to then delete selected item, so it doesn't show in listbox next time macro is ran?

which way do you think is easiest to go about this?

I don't necessarily need to delete from the actual sheet if I don't need to?
i just want the selected item gone from listbox, for next macro run.

what would you recommend?
 
Last edited:
Upvote 0
Norie, is there a way I can maybe describe this better?

i add my items as shown above, and then i need this edited so it deletes the selected value from the listbox and updates, it only deletes the cell(and listbox value) when first value is chosen.

Code:
Private Sub KButton_Click()
ActiveCell.Value = "KR " & KList.Value
Sheets("Source").Cells(KList.ListIndex + 6).Delete xlShiftUp
Unload Me
End Sub
 
Upvote 0
just looking for some guidance, if i have a column with data in it for 80 cells.

i want to put all that data in a listbox. be able to select it, input it where i want(activecell) then delete it, so my listbox is updated next time i run macro.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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