【问题标题】:Excel VBA ListBox Total in Currency Format货币格式的 Excel VBA 列表框总计
【发布时间】:2021-08-25 01:30:00
【问题描述】:

如何将总列值以货币表示(例如 $27265,28),搜索我找到了代码 .AddItem Format(Cell.Value, "$#,##0.00") 但它不起作用,我需要将 Listbox 列格式化为货币 ("$#,##0.00).

Private Sub CommandButton1_Click()

Unload Me

End Sub


Private Sub Label1_Click()

End Sub

Private Sub TextBox1_Change()
Me.TextBox1 = Format(StrConv(Me.TextBox1, vbLowerCase))

Dim sh As Worksheet
Set sh = Sheets("Sheet1")
Dim i As Long
Dim x As Long
Dim p As Long
Me.ListBox1.Clear

For i = 2 To sh.Range("B" & Rows.Count).End(xlUp).Row
For x = 1 To Len(sh.Cells(i, 2))
p = Me.TextBox1.TextLength

If LCase(Mid(sh.Cells(i, 2), x, p)) = Me.TextBox1 And Me.TextBox1 <> "" Then
With Me.ListBox1

.AddItem sh.Cells(i, 2)

.List(ListBox1.ListCount - 1, 1) = sh.Cells(i, 3)

.List(ListBox1.ListCount - 1, 2) = sh.Cells(i, 4)

.List(ListBox1.ListCount - 1, 3) = sh.Cells(i, 5)

.List(ListBox1.ListCount - 1, 4) = sh.Cells(i, 6)

.List(ListBox1.ListCount - 1, 5) = sh.Cells(i, 7)

End With


End If

Next x

Next i


End Sub

【问题讨论】:

  • cstr(Format(cell.Value, "$#,##0.00")) 有效吗?我已经试过你的代码,它对我来说没问题。
  • 顺便说一句,您应该在End WithEnd If 之前插入一个Exit For,以避免可能多次显示同一事件。 @MarcMelo
  • 我忘了告诉你它在列表框中,但感谢你的时间帮助我

标签: excel vba listbox


【解决方案1】:

应该是这样的:

sh.Columns("C").NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"

正如文档https://docs.microsoft.com/en-us/office/vba/api/excel.range.numberformat中提到的那样

【讨论】:

  • 我忘了告诉你它在列表框中
  • 然后Format(sh.Cells(i, 3), "$#,##0.00")
  • 非常感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
相关资源
最近更新 更多