【问题标题】:vba number format write specific textvba数字格式写具体文字
【发布时间】:2017-05-06 20:35:49
【问题描述】:

我在使用 VBA for Excel 方面很新,但经过一番搜索但没有结果,我想我需要你的帮助。

我正在尝试格式化特定单元格,以便在满足一个条件时显示来自另一个单元格的值,但前面是特定文本,例如“Constantin”。因此,显示的结果将是,例如 Constantin 80.25 。

我正在尝试的代码如下所示:

    If Cells(4, 1) < 0 Then

    With Range("A1")
    .NumberFormat = "'Constantin' 0.00"
    .Value = - Cells(4, 1)

    End With

    End If

我知道语法不正确,这是我的问题,我想我找不到正确的语法。我希望我不会用这个可能很简单的问题来打扰你,但我就是找不到我需要的东西。非常感谢

【问题讨论】:

  • With 中的两行替换为以下.Value = "Constantin" &amp; Cells(4, 1).Value

标签: vba custom-formatting


【解决方案1】:

看起来您不需要将单词作为实际格式的一部分,在这种情况下,这样的内容就足够了:

If Cells(4, 1).Value < 0 Then
    Range("A1").Value = "Constantin " & (Cells(4, 1).Value * -1)
End If

如果您真的想使用With 块,那么:

With Cells(4, 1)
    If .Value < 0 Then
        Range("A1").Value = "Constantin " & (.Value * -1)
    End If
End With

【讨论】:

  • 好的,现在好像可以了。还有一个问题——例如,我怎样才能将小数位数减少到一位?现在我得到一个 13 位小数的数字。谢谢
  • "Constantin " &amp; WorksheetFunction.Round(.Value * -1, 1) 应该可以解决问题 ;)
  • 我在一个模块中编写了我的代码,这可能是我在选择“.value”的情况下收到此错误“无效或不合格的引用”的原因,当我尝试使用该行编译代码时你建议
  • 整行应该是Range("A1").Value = "Constantin " &amp; WorksheetFunction.Round((.Value * -1), 1)
  • 'If Cells(4, 1).Value
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多