excel - 使用货币格式设置多范围列表框的格式

标签 excel vba listbox format userform

我正在尝试使用货币格式(“$#,##0.00”)格式化列表框的第二列,但遇到了一些麻烦。任何帮助将不胜感激!

这是一些测试数据:

Data

Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("PivotTable")

Dim rng1 As Range
Dim LR1 As Long
LR1 = Range("A" & Rows.Count).End(xlUp).Row
Set rng1 = ws1.Range("A1:A" & LR1).SpecialCells(xlCellTypeVisible)

With Me.ListBox1
    .ColumnCount = 2
    .ColumnWidths = "120,100"

    For Each Cell In rng1
        .AddItem Format(Cell.Value, "$#,##0.00")
        .List(.ListCount - 1, 1) = Cell.Offset(0, 1).Value
        .List(.ListCount - 1, 2) = Cell.Offset(0, 2).Value 'Format this column        
    Next Cell
End With

这是我现在得到的结果:

Error Result

最佳答案

我认为@T.M.是对的。请尝试以下操作:

For Each Cell In rng1

    .AddItem Cell.Value 'this is your first column
    .List(.ListCount - 1, 1) = Format(Cell.Offset(0, 1).Value, "$#,##0.00") 'this is your second one
    'you tried to format the 3rd one (which was not visible because of .ColumnCount = 2:
    '.List(.ListCount - 1, 2) = Cell.Offset(0, 2).Value 'Format this column
Next Cell

说明:

.AddItem 填充第一列。 .List(row, column) 中的列计数以 0 开头,因此 .AddItem 填充列 0 这意味着 .List(.ListCount - 1, 1) 是您的第二列(不是第一列)。

关于excel - 使用货币格式设置多范围列表框的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632405/

相关文章:

Excel for Mac 2016 - 另存为 CSV - 行尾错误

excel - 查找某个月份的 MAX 值

excel - 选择某些选项时,是否有用于自动填写价格的 Excel 公式?

excel - ListBox 中的多选值可粘贴到另一张工作表的不同单元格中

c# - 通过单击更改 MySQL 中的 listBox 内容

javascript - 为什么我不能用 VBA 访问这个弹出框的内容?

excel - 删除列表框上的滚动条

excel - 在excel VBA中使用IF和And的组合

vba - Excel-VBA .Range ("NamedRange").Row 返回不同的行然后范围所在的位置

wpf - ListBox中的滚动效果