带偏移列的VBA ListBox

时间:2017-10-05 18:45:41

标签: vba listbox offset userform

我正在使用以下代码填充ListBox。 ListBox的第1列来自我的工作表的F列,而ListBox的第2列来自下一列。但我想让第二个ListBox列来自工作表的D列(左侧2)。我只是无法按照我想要的方式工作。

    Private Sub UserForm_Initialize()

    Dim dic As Object
    Dim rng As Range
    Dim ky As Variant

    Set dic = CreateObject("Scripting.Dictionary")

    Set rng = Sheet4.Range("F2")

    Do
        If Not dic.exists(rng.Value) Then
            dic.Add rng.Value, rng.Offset(, 1).Value
        Else
            dic(rng.Value) = dic(rng.Value) + rng.Offset(, 1).Value
        End If
        Set rng = rng.Offset(1)
    Loop Until rng.Value = ""

    For Each ky In dic.keys
        With BWListBox3
            .AddItem
            .List(.ListCount - 1, 0) = ky
            .List(.ListCount - 1, 1) = Application.Text(dic(ky), "[h]:mm") 
        End With
    Next ky

End Sub

1 个答案:

答案 0 :(得分:1)

左边2?

    If Not dic.exists(rng.Value) Then
        dic.Add rng.Value, rng.Offset(, -2).Value
    Else
        dic(rng.Value) = dic(rng.Value) + rng.Offset(, -2).Value
    End If
相关问题