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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
Only deleting correctly from where?

The listbox or the worksheet?

Are you actually deleting the selected item from the listbox?
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200

ADVERTISEMENT

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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
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.
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200

ADVERTISEMENT

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:

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
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
 

egraham3

Board Regular
Joined
Jun 14, 2010
Messages
200
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.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,732
Messages
5,833,358
Members
430,206
Latest member
Sami Gaid

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
Top