0

I have created a macro file with Forms and Word to Excel.

In this coding fewthings are not working as per my expectation.

  1. Get unique Employee Name from Excel data base. I want to add unique employee names from excel database and get is saved in a sheet. After that those values to be added to list box. Here i cannot define a range like "A1:A10".. I want to choose the data from A1 to end data.

If for each cell approach will not work, please help in do while approach

I need help in defining the range and code given below

ListEmployeeName.Clear
For Each cell In Worksheets("SunEmployeeDetails").Range("A1").End(xlDown)
    ListEmployeeName.AddItem (cell.Value)
Next
    ListEmployeeName.Value = Worksheets("SunEmployeeDetails").Range("A1")
End Sub
sundar
  • 1
  • ListEmployeeName.Clear Worksheets("SunEmployeeDetails").Select Range("A1").Select Do While ActiveCell.Value <> "" ListEmployeeName.AddItem (cell.Value) ActiveCell.Offset(1, 0).Select Loop ListEmployeeName.Value = Worksheets("SunEmployeeDetails").Range("A1") End Sub This do while approach is also not working – sundar Dec 25 '19 at 18:45
  • Avoid the use of `.Select/Activecell` etc... you may want to see [How to avoid using Select in Excel VBA](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba). I have posted an answer. you may have to refresh the page to see it – Siddharth Rout Dec 25 '19 at 18:53
  • [This](https://stackoverflow.com/q/59390356/9758194) could be a cool alternative in your case to get some uniques – JvdV Dec 25 '19 at 20:37

2 Answers2

1

Find Last Row and then define your range Range("A1:A" & LastRow)

You can also find the last row and loop through the range using a For loop. Also to get unique Employee Name, you can use On Error Resume Next with a Collection as shown below. I have commented the code below so you should not have a problem understanding it. But if you do then simply ask.

Is this what you are trying? (Untested).

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long
    Dim col As New Collection
    Dim itm As Variant

    Set ws = Worksheets("SunEmployeeDetails")

    With ws
        '~~> Find Last row
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        '~~> Loop through the range and add it to the unique
        '~~> collection using "On Error Resume Next"
        For i = 1 To lRow
            On Error Resume Next
            col.Add .Range("A" & i).Value, CStr(.Range("A" & i).Value)
            On Error GoTo 0
        Next i
    End With

    ListEmployeeName.Clear

    '~~> add the itme from collection to the listbox
    For Each itm In col
        ListEmployeeName.AddItem itm
    Next itm
End Sub
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Pretty sure you can get a 1D array assigned to a listbox in one go right? Using .List property..... Btw, not used to see untested codes from your end =) – JvdV Dec 25 '19 at 20:39
  • 1
    @JvdV: Oh yes. I have shown that [Here](https://stackoverflow.com/questions/59316810/vba-close-unwrap-retract-a-combobox-list) as well but did not mention here becuase it would be an overkill after trying to get a unique collection. – Siddharth Rout Dec 26 '19 at 04:59
  • Nice linked post. Didn't know about a bug. I'll have a bash with that combined with what I linked to earlier – JvdV Dec 26 '19 at 08:14
0

Here is my take on it, techniques taken from here:


Methode 1: Using a dictionary

Dim lr As Long, x As Long
Dim arr As Variant
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")

With Sheet1 'Change accordingly

    'Find the last used row
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row
    arr = .Range("A1:A" & lr).Value

End With

'Loop through memory and fill dictionary
For x = LBound(arr) To UBound(arr)
    dict(arr(x, 1)) = 1
Next x

'Add array to Listbox
Me.ListEmployeeName.List = dict.Keys

Methode 2: Using Evaluation

Dim lr As Long
Dim arr As Variant

With Sheet1 'Change accordingly

    'Find the last used row
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row

    'Get array of unique values
    arr = Filter(.Evaluate("TRANSPOSE(If(Row(A1:A" & lr & ")<>MATCH(A1:A" & lr & ",A1:A" & lr & ",0),""|"",A1:A" & lr & "))"), "|", False)

    'Add array to Listbox
    Me.ListEmployeeName.List = arr

End With
JvdV
  • 70,606
  • 8
  • 39
  • 70