具有百分比的Userform列表框?

时间:2015-11-17 15:29:29

标签: excel vba excel-vba listbox userform

我在userform中使用了一个列表框,它显示了一个格式为百分比的列中的唯一值。我通过使用字典对象并将其提供给列表框来生成唯一的列表/值。

我的问题是所有值都显示为列表框中的十进制数而不是百分比。知道如何以百分比/格式显示它们吗?

仅供参考:列表框值可以更改,具体取决于用户选择文本/数字/日期/货币/百分比

唯一列表代码

Sub UniqData(fString As String, cbNr As Integer) ' fString as string
Dim d As Object

With Sheets("xxx")
cNr = WorksheetFunction.Match(fString, .Rows(1), 0)

lRo = .Cells(Rows.Count, 1).End(xlUp).Row
arrD = .Range(.Cells(2, cNr), .Cells(lRo, cNr))
    Set d = CreateObject("scripting.dictionary")
     For Each c In arrD
            If Len(c) > 0 Then
                d00 = dic.Item(c.Text)
            End If
      Next c
    k = d.keys
End With

UserForm1.Controls("lb" & cbNr).List = k

End Sub
像这样。

2 个答案:

答案 0 :(得分:1)

正如@Meehow所提到的,如果数据太大而无法在单元格中显示,则使用c.Text可能会产生错误!

因此,在Format(c.Value, "0.0%")行中使用d.Add作为键,您将获得Excel单元格中的格式:     Sub UniqData(fString As String,cbNr As Integer)'fString as string     Dim d As Object

With Sheets("xxx")
    cNr = WorksheetFunction.Match(fString, .Rows(1), 0)

    lRo = .Cells(Rows.Count, 1).End(xlUp).Row
    arrD = .Range(.Cells(2, cNr), .Cells(lRo, cNr))
    Set d = CreateObject("scripting.dictionary")
    For Each c in arrD
        d.Add Format(c.Value, c.NumberFormat), c.Value
    Next c
    k = d.keys
End With

UserForm1.Controls("lb" & cbNr).List = k

End Sub

答案 1 :(得分:1)

使用电子表格格式化单元格只会修改数字在视觉上呈现给用户的方式。基础价值保持不变。

因此,在将字典添加到字典时,需要将值显式格式化为您希望它的样子。

d.Add Format(c, "0.0%"), c 
相关问题