In VBA code,how to add values to a listbox using for each cell approach with defining a range End(XlDown)?

瘦欲@ 提交于 2020-01-24 00:51:07

问题


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

回答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



回答2:


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


来源:https://stackoverflow.com/questions/59480927/in-vba-code-how-to-add-values-to-a-listbox-using-for-each-cell-approach-with-def

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!