excel vba - VBA - Preselecting Userform Listbox values matched from a string separated by commas -


1) have column of data fills multiselect listbox in userform. values days of week "monday" - "sunday"

dim listssheet worksheet dim lastrow long dim acell range  set listssheet = sheets("lists") listssheet     'fills days     lastrow = .cells(.rows.count, "a").end(xlup).row     each acell in .range("a2:a" & lastrow)         if acell.value <> ""         me.day.additem acell.value         end if     next 

2) i'm able gather multi-selected values , transfer worksheet manipulation. assuming user selected days "tuesday" + "wednesday", data in cells = "tuesday, wednesday"

3) i'm attempting recreate listbox selected user. able use .additem generate list of days again, i'm unsure how go selecting correct .listindex value based on initial selection

the selection make in listbox recorded in zero-based boolean array has many elements listbox has items. array accessible listbox's read/write selected property. in case listbox1.selected(0) refers "monday" , listbox1.selected(6) sunday.

listbox1.selected(0) true or false depending upon whether monday selected or not. can read property when close form , re-establish same setting setting listbox1.selected(0) = true (or false) when show form again.

since have weekday names in a2:a8 might write selected() array b2:b8 in real time using listbox1_change event, , use range both on sheet , reset listbox.

by way, way of setting list isn't efficient. one, want 7 items in list. therefore

lastrow = .cells(.rows.count, "a").end(xlup).row 

will cause more mistakes prevents. another, excel's vba has provided rowsource property. can set list setting property. both following examples correct.

listbox1.rowsource = activesheet.range("a2:a8").address listbox1.rowsource = "dayslist"     ' dayslist named range 

the use of activesheet safe if call form worksheet , therefore know 1 active @ time. or can apply array directly list property, this:-

listbox1.list = split("mon tue wed thu fri sat sun", " ") 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -