VBA Listbox: Check if listbox items are already in specified worksheet range and write listbox item in next empty cell -
title seems bit complicated, i'll try explaining steps (what want vba code do):
- read 1 list item list
- if item in 1st column (columns(1)) in worksheet, skip steps 3. , 4.
- if item not in 1st column (columns(1)) check first empty cell in column (reading top)
- when next empty cell has been found copy item not in column in empty cell
- proceed next item
what have far following code:
private sub tab1_done_button_click() dim ws worksheet dim integer dim listitem variant listitem = 0 me.tab1_product_picked.listcount - 1 'check if product there (dont't create duplicate) = 4 400 if ws.cells(i, 1).value <> me.tab1_product_picked.list(listitem) 'write in first empty cell in 1st column if not duplicate each cell in ws.columns(1).cells if isempty(cell) = true cell.value = me.tab1_product_picked.list(listitem) exit end if next cell exit end if next next listitem end sub
i have made logical mistake somewhere here, happens following:
i have product 3, product 5 , product 6 in first column. go listbox in form , after several actions end following items in listbox: product 3, product 6 , product 7. expect code transfer product 7 in next empty cell in 1st column, currenlty still transfers products listbox , after have in first column: product 3, product 5, product 6, product 3, product 6, product 7. means code able recognize empty cells in first row, able read data listbox, able copy data listbox , paste in empty cells desired, but unable check whether product listbox in 1 of cells in 1st column.
thank in advance advice on how fix part code needs check duplicates.
cheers , thanks!
i have rewrote code in way, supposed to. tested , works correctly:
range("a:a").select listitem = 0 me.tab1_product_picked.listcount - 1 if selection.find(what:=me.tab1_product_picked.list(listitem), lookin:=xlvalues) nothing each cell in columns(1).cells if isempty(cell) = true cell.value = me.tab1_product_picked.list(listitem) exit end if next cell end if next listitem
i'll leave here in case ever encounters same problem :)
Comments
Post a Comment