Indexing in VBA .additem

SamirBhowmik

New Member
Joined
Sep 23, 2021
Messages
26
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Can anyone help me with the below piece of code:

Private Sub UserForm_Initialize()

Dim mylist As Collection
Dim myrng As Range
Dim ws As Worksheet
Dim myval As Variant

Set ws = ThisWorkbook.Sheets("MDB")
Set myrng = ws.Range("A2", ws.Range("A2").End(xlDown))
Set mylist = New Collection

On Error Resume Next

For Each mycell In myrng.Cells
mylist.Add mycell.Value, CStr(mycell.Value)
Next mycell

On Error GoTo 0

For Each myval In mylist
Me.ListBox1.AddItem myval

Next myval
End Sub

This code populates my first listbox with unique items from column A from a table. I have couple of other listboxes where I want to refer to other columns and populate the unique values. I am a newbi in vba so any assistance with explanation will be really appreciated
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

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