scrolling automatically to last row in ListBox

sathyaganapathi

Board Regular
Joined
Apr 29, 2021
Messages
81
Office Version
  1. 2016
Platform
  1. Windows
I want ListBox to show the Last item automatically when new item is added.
One suggestion was to use code "ListBox1.TopIndex = ListBox1.Items.Count - 1". But when used, I am getting compile error saying "Method or data member not found" and word ".Items" is highlighted.
How to get rid of this error
Or, is there any other way to go to last line in the Listbox automatically?
Please help.
Thanks.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
VBA Code:
Private Sub CommandButton2_Click()
    With Me.ListBox1
        .AddItem "item " & .ListCount
        .TopIndex = .ListCount - 1
        .ListIndex = .ListCount - 1
    End With
End Sub
This works for sure - see if it does what you need.
 
Upvote 0
VBA Code:
Private Sub CommandButton2_Click()
    With Me.ListBox1
        .AddItem "item " & .ListCount
        .TopIndex = .ListCount - 1
        .ListIndex = .ListCount - 1
    End With
End Sub
This works for sure - see if it does what you need.
Hi bobsan42,
sorry to bother you again.
Does it work by clicking a commandbutton?

I assigned the the code to a commandbutton.
I am getting error saying "permission denied" (run_time error '70') on the below line
.AddItem "item " & .ListCount
 
Upvote 0
You can make it work as you like - command button click is OK for testing purposes, but it can be included in any procedure and started as you like.
The reason for RTE 70 is probably that your list has a RowSource assigned - the list shows the contents of a range on a sheet. So you cannot add items manually.

so to only test the scroll and select to the last item use this:
VBA Code:
Private Sub CommandButton2_Click()
    With Me.ListBox1
        .TopIndex = .ListCount - 1
        .ListIndex = .ListCount - 1
    End With
End Sub
 
Upvote 0
Solution
You can make it work as you like - command button click is OK for testing purposes, but it can be included in any procedure and started as you like.
The reason for RTE 70 is probably that your list has a RowSource assigned - the list shows the contents of a range on a sheet. So you cannot add items manually.

so to only test the scroll and select to the last item use this:
VBA Code:
Private Sub CommandButton2_Click()
    With Me.ListBox1
        .TopIndex = .ListCount - 1
        .ListIndex = .ListCount - 1
    End With
End Sub
Hi bobsans42,
this made my day...!!!! Thanks a lot for the perfect solution for which I was struggling for hours !! your solution works perfectly with my code. (y)
Thanks again and good day :)
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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