Refresh rowsource of a listbox

thanasis380

New Member
Joined
Oct 6, 2017
Messages
3
I have a range (Myrange) with three columns, the first two (eg Name, Address) are filled and the third (eg Age) will be filled through a form.
So I made a listbox with rowsource (Myrange) and the user will select his name and then an inputbox will ask for a numeric value.
The value is inserted in Myrange in the sheet and simultaneously the rowsource of the listbox is refreshed.
Unfortunately my code asks twice for the age. Any suggestions to solve that?
Thanks in advance.

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; white-space: inherit;">PrivateSub UserForm_Initialize()
With ListBox1
.ColumnCount =3
.ColumnHeads =True
.ListStyle = fmListStyleOption
.MultiSelect = fmMultiSelectSingle
.RowSource ="Sheet1!A2:C6"
.ListIndex =-1
EndWith
EndSub

PrivateSub ListBox1_Click()
Dim Myrange AsString
Myrange = ListBox1.RowSource

For i =0To ListBox1.ListCount -1
If ListBox1.Selected(i)=TrueThen
ExitFor
Else
EndIf
Next i

x
= Application.InputBox("Insert age")
y
= ListBox1.ListIndex

ListBox1
.RowSource = vbNullString
Sheets("Sheet1").Cells(y +2,3).Value = x
ListBox1
.RowSource = Myrange
EndSub</code>
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Rather than using a listbox click event, add a command button & run the code that way.
Code:
Private Sub CommandButton1_Click()

Dim Myrange As String

Myrange = ListBox1.RowSource

For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
Exit For
Else
End If
Next i

x = Application.InputBox("Insert age")
y = ListBox1.ListIndex

ListBox1.RowSource = vbNullString
Sheets("Page1").Cells(y + 2, 3).Value = x
ListBox1.RowSource = Myrange
End Sub
 
Upvote 0
Thank you Fluff for your reply.
It works, but my original idea was to have one click (only selecting the row) instead of two (selecting row and clicking button).
Do you have any idea why the inputbox appears twice?
 
Upvote 0
Your last line of code is updating the listbox, which is acting as a click event & therefore causes the event to run again.
 
Upvote 0
Another option is
Code:
Private Sub ListBox1_AfterUpdate()
Dim Myrange As String

Myrange = ListBox1.RowSource

[COLOR=#0000ff]'For i = 0 To ListBox1.ListCount - 1
'If ListBox1.Selected(i) = True Then
'Exit For
'Else
'End If
'Next i[/COLOR]

x = Application.InputBox("Insert age")
y = ListBox1.ListIndex

ListBox1.RowSource = vbNullString
Sheets("Page1").Cells(y + 2, 3).Value = x
ListBox1.RowSource = Myrange

End Sub
Also, the lines in blue are redundant, so you can delete them.
 
Upvote 0
This is another option :-
Code:
Private [COLOR="Navy"]Sub[/COLOR] UserForm_Initialize()
[COLOR="Navy"]With[/COLOR] ListBox1
    .ColumnCount = 3
    .ColumnHeads = True
    .ListStyle = fmListStyleOption
    .MultiSelect = fmMultiSelectSingle
    .List = Sheets("Sheet1").Range("A2:C6").Value
    .ListIndex = -1
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]

Code:
Private [COLOR="Navy"]Sub[/COLOR] ListBox1_Click()
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] X [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Rw [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]For[/COLOR] i = 0 To ListBox1.ListCount - 1
    [COLOR="Navy"]If[/COLOR] ListBox1.Selected(i) = True [COLOR="Navy"]Then[/COLOR]
        Rw = i
        X = Application.InputBox("Insert age")
        ListBox1.List(Rw, 2) = X
        Sheets("Sheet1").Cells(Rw + 2, 3).Value = X
        [COLOR="Navy"]Exit[/COLOR] For
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] i
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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