Repopulate Listbox after Update

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
832
Office Version
  1. 365
Platform
  1. Windows
After transferring Data to a Sheet from a listbox it fills it back with a named range
Me.listbox1.list = Range("lboxRange").Value

When it fills the listbox with updated Values I want it to select the Value that was updated to show the change

Instead it goes to a selection which is top of that range

i.e.
Listbox has 4 columns Category, ID, Name and Grade

If I update Fred to an Asst Manager When the listbox updates it highlights Barbara's Row which is the top of B Category. I thought it might have gone right to the top, not sure why it highlights the top Row of a Category

Likewise if I update Zeb (Category C) when updated it highlights Amy's Row

Ideally I want it to highlight the Row that was updated
CategoryIDNameGrade
A
1​
AlanManager
A
2​
MaryManager
A
3​
KevinClerk
B
4​
BarbaraClerk
B
5​
Stewartclerk
B
6​
Fredclerk
C
7​
AmyAsst Manager
C
8​
ZachAsst Manager
C
9​
ZebSupervisor
D
10​
BobSupervisor
D
11​
HenryManager
D
12​
JackManager
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
After transferring Data to a Sheet from a listbox it fills it back with a named range
I guess you have some textboxes and with a button you read the data from the textbox and pass that data to the sheet.
You could put the code you use to transfer the data to the sheet.
 
Upvote 0
VBA Code:
Private Sub listbox1_click()
Textbox1.value = listbox1.column(2)
End sub

Private Sub CommandButton1_click()
Range("C10").value = textbox1.value
Me.listbox1.list = Range("lboxRange").Value
End sub
 
Upvote 0
With the code you provided in the previous post, if I select a record and press the button, the sheet is updated and the item highlighted in the listbox, it remains the same updated record.
You have more codes in the form, put here all the code of your form.
 
Upvote 0
VBA Code:
Private Sub UserForm_Initialize()
ListBox1.List = Range("lboxRange").Value

End Sub

Private Sub listbox1_click()
TextBox1.Value = ListBox1.Column(2)
End Sub

Private Sub CommandButton1_click()
Range("C10").Value = TextBox1.Value
ListBox1.List = Range("lboxRange").Value

' When I Click the Command Button to refill the Listbox it selects the Top Category
' i.e. If I select B 6 Fred clerk and then Run it will Select B 4 Barbara Clerk
' Which is the top one in B Category
End Sub
 
Upvote 0
By default the value of the BoundColumn property of the listbox is 1.
The BoundColumn property specifies which item from the row is stored as the value.

So when you select the record with the letter "B" (Fred), the value of the listbox is "B"; When you reload the listbox, what the listbox index does is select the first value it finds with the letter "B" in column 1.

1637671149562.png


To keep the record selected, change the BoundColumn property to 0.

1637671108832.png
 

Attachments

  • 1637670651267.png
    1637670651267.png
    5 KB · Views: 3
Upvote 0
Solution
Thanks

I tried to set in Listbox Click or initialisation and it crashed and closed for both

Listbox1.BoundColumn = 0

Run-time Error '-2147417848(80010108)':
Automation error
The object invoked has disconnected from its clients

It did work when Manually setting in properties
 
Upvote 0
I tried to set in Listbox Click or initialisation and it crashed and closed for both
I don't have that problem.

VBA Code:
Private Sub UserForm_Initialize()
  ListBox1.BoundColumn = 0
  ListBox1.List = Range("lboxRange").Value
End Sub

It did work when Manually setting in properties
Work with that option.
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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