VBA Code - Select last entry of a listbox ?

JumboCactuar

Well-known Member
Joined
Nov 16, 2016
Messages
785
Office Version
  1. 365
Platform
  1. Windows
Hi,
i have this code

Code:
Private Sub UserForm_Initialize()
    Dim MyData As Range
    Dim n As Integer
    Dim r As Long


   With ThisWorkbook.Sheets("Main").DropDowns("ComboBox1")
      If .Value > 0 Then Call MacroX(.List(.Value))
   End With
Label1.Caption = Sheets("Report").Range("C1")
Label2.Caption = Sheets("Report").Range("B17")
End Sub




Sub MacroX(sName As String)
Application.ScreenUpdating = False
            With Sheets("Sheet1")
        .Range("$A$1:$M$5000").AutoFilter Field:=3, Criteria1:=sName
        .Columns("A:H").Copy Sheets("Report").Range("A21")
    End With
                Sheets("Sheet1").Range("$A$1:$M$5000").AutoFilter Field:=3
        Sheets("Report").Range("A22:A5000").ClearFormats
         Sheets("Report").Range("C22:H5000").ClearFormats
    With Me.ListBox1
        .RowSource = ""
        .ColumnCount = 6


        Set MyData = Worksheets("Report").Range("B21:G3000")   'Adjust the range accordingly
        .List = MyData.Cells.Value
        For r = .ListCount - 1 To 0 Step -1
            If .List(r, 3) = "" Then
                .RemoveItem r
            End If
        Next r
    End With


For n = 0 To ListBox1.ListCount - 1
With ListBox1
  .List(n, 0) = Format(.List(n, 0), "hh:mm")
End With
Next n


    Application.ScreenUpdating = True
End Sub

What it does is filter data on sheet 1 based on dropdown value (name)
Copies it to a user report sheet and the data is viewable from a userform

But im wanting on userform initialise for it to select the last entry, does anyone know how?

thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,213,495
Messages
6,113,992
Members
448,538
Latest member
alex78

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