Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 使用货币格式设置多范围列表框的格式_Excel_Vba_Listbox_Format_Userform - Fatal编程技术网

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

Excel 使用货币格式设置多范围列表框的格式,excel,vba,listbox,format,userform,Excel,Vba,Listbox,Format,Userform,我试图用货币格式($),##0.00)格式化列表框的第二列,但遇到了一些麻烦。非常感谢您对我们的任何帮助 以下是一些测试数据: 这就是我现在得到的结果: 我认为@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") '

我试图用货币格式($),##0.00)格式化列表框的第二列,但遇到了一些麻烦。非常感谢您对我们的任何帮助

以下是一些测试数据:

这就是我现在得到的结果:


我认为@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
so
开头。AddItem
填充列
0
,意思是
。List(.ListCount-1,1)
是第二列(不是第一列)。

Format
仅对数值有效,而不对
字符串有效。您无法格式化需要将字符串切块以获得可以格式化的数值的字符串。您的问题是
.List(.ListCount-1,2)
不引用第二列列表框,而是引用可能的第三列,因为列表索引是零边界的,并且无法将添加的列表行格式化为一个完整的集合(默认情况下,使用
AddItem
)可以为10列中的每一列指定单一格式的字符串:-)谢谢!我成功了@Andrew,不,您必须将从
List(.Listcount-1,0)
开始的每个元素格式化为第一个元素,将
List(.Listcount-1,2)
格式化为第二个元素
.List(.ListCount-1,3)
甚至会引用第四列列表。-参见Peh的答案::-)@Andrew注意到您应该在
LR1=Range(“A”&Rows.Count)
中指定工作表,就像
LR1=ws1.Range(“A”&ws1.Rows.Count)
一样,否则它可能选择了错误的工作表。@PEH-就像旁注一样:
。AddItem
默认情况下甚至会添加10个空列元素(然而,使用整个数据集的数组分配方法不限于该固定数量限制)。@t.M.是的,正确!我将把“添加第一列”更改为“填充第一列”。@Andrew FYI:-)关于所述数组方法的可能进一步阅读:
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