【问题标题】:How to Change Multiple Format Parameters of a Cell Based on Its Value?如何根据单元格的值更改单元格的多个格式参数?
【发布时间】:2019-02-03 03:48:17
【问题描述】:

我想编写一个代码,根据单元格值更改选定单元格范围的样式属性。

当我只更改文本颜色或字体时它起作用了,但是如果代码什么都不做,我会为每个参数添加多个参数。我也没有收到错误。

Dim userRange As Range

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In Selection

        If cell.Value < 0 Then cell.Font.FontStyle = "Comic Sans MS" & cell.Font.Size = 18 & cell.Font.Color = vbRed

        If cell.Value >= 0 And cell.Value <= 500 Then cell.Font.Bold = True & cell.Font.Italic = True & cell.Font.Underline = True

        If cell.Value > 500 And cell.Value <= 1000 Then cell.Font.FontStyle = "Monotype Corsiva" & cell.Font.Color = vbBlue & cell.Font.Underline = xlUnderlineStyleDouble

        If cell.Value > 1000 Then cell.Font.FontStyle = "Arial" & cell.Font.Bold = True & cell.Font.Italic = True & cell.Interior.Color = vbGreen & cell.Font.Color = vbWhite

Next cell

我想我真的很接近,但我似乎无法弄清楚我做错了什么!我希望我的解释很清楚,因为我真的不习惯编程/编写脚本。

提前致谢!

【问题讨论】:

  • 如果你真的想在一个代码行中添加连续的赋值,你必须使用 : 字符作为行分隔符(但是,这不是 logical AND 替换) 而不是与符号&amp;。否则,在If 条件中使用单独的代码行更易读。
  • 好吧,我想在我写这样的代码之前我需要多学习一点!谢谢你的提示!我已经在想我的代码看起来有点乱了!
  • 请注意,如果您需要多次运行并且值可以在运行之间更改,则需要先清除/重置 所有 样式属性...

标签: excel vba


【解决方案1】:

我认为这应该解决它。您的旧代码没有执行每一行。您必须插入: 的空格,而不是&amp;。此外,如果您使用With 功能来节省一些打字,它会节省一些打字。另请注意,您使用的是ActiveCell,请确保这是故意的。

Dim userRange As Range, d As Double, cell As Range 'added more variables

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In userRange.Cells
    d = cell.Value

    With cell.Font

        If d < 0 Then
            .FontStyle = "Comic Sans MS"
            .Size = 18
            .Color = vbRed
        End If


        If d >= 0 And d <= 500 Then
            .Bold = True
            .Italic = True
            .Underline = True

        End If


        If d > 500 And d <= 1000 Then
            .FontStyle = "Monotype Corsiva"
            .Color = vbBlue
            ActiveCell.Underline = xlDouble ' is this right?
        End If


        If d > 1000 Then
            .FontStyle = "Arial"
            .Bold = True
            .Italic = True
            .Color = vbGreen 'this is being undone in the next line of code.
            .Color = vbWhite 
        End If

    End With

Next cell

【讨论】:

  • 我使用了这个代码,谢谢!它更干净,效果很好!
【解决方案2】:

您定义了 userRange,但后来您在选择的单元格上循环。此外,您使用 & 不正确。你可以试试这个:

Dim userRange As Range

Set userRange = Application.InputBox("Select a range", Type:=8)

For Each cell In userRange

        If cell.Value < 0 Then
            cell.Font.FontStyle = "Comic Sans MS"
            cell.Font.Size = 18 & cell.Font.Color = vbRed
        End If

        If cell.Value >= 0 And cell.Value <= 500 Then
            cell.Font.Bold = True & cell.Font.Italic = True
            cell.Font.Underline = True
        End If

        If cell.Value > 500 And cell.Value <= 1000 Then
            cell.Font.FontStyle = "Monotype Corsiva"
            cell.Font.Color = vbBlue
            cell.Font.Underline = xlUnderlineStyleDouble
        End If

        If cell.Value > 1000 Then
            cell.Font.FontStyle = "Arial"
            cell.Font.Bold = True
            cell.Font.Italic = True
            cell.Interior.Color = vbGreen
            cell.Font.Color = vbWhite
        End If
Next cell

【讨论】:

  • 这看起来与我的答案非常相似,但我会给你打勾,因为我要争取“我是一个好公民”的徽章!”
  • 我开始在 excel 模块中处理这个问题,但在我有机会粘贴我的答案之前,你已经在这里了。无论如何,谢谢,也拿一个。
  • 谢谢你的作品!我想我会阅读更多关于如何使用 & 函数的内容。我还编辑了应该是 .Interior.Color 的双 .Font.Color
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多