简体   繁体   English

如何根据其值更改单元格的多种格式参数?

[英]How to Change Multiple Format Parameters of a Cell Based on Its Value?

I want to write a code that changes the style properties of a range of selected cells based on the cell value. 我想编写一个基于单元格值更改选定单元格范围的样式属性的代码。

It worked when I only changed the text-color or font but since I added multiple arguments to each if the code does nothing. 当我仅更改文本颜色或字体时有效,但由于代码不执行任何操作,因此我向每个参数添加了多个参数。 I also don't get an error. 我也没有得到错误。

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

I think I'm really close but I can't seem to figure out what I'm doing wrong! 我想我真的很接近,但我似乎无法弄清楚自己在做什么错! I hope my explanation is clear since I'm not really used to programming/scripting. 我希望我的解释很清楚,因为我不太习惯编程/脚本编写。

Thanks in advance! 提前致谢!

I think this should fix it. 我认为这应该解决它。 Your old code wasn't executing each line. 您的旧代码并未执行每一行。 You have to insert a space of a : instead of an & . 您必须插入:而不是&的空格。 Also, it saves some typing if you use the With feature to save some typing. 此外,如果您使用“ With功能来保存一些键入内容,它还会保存一些键入内容。 Also take note that you are using ActiveCell , make sure that's intentional. 还要注意,您正在使用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

You defined userRange but later on you are looping over cells in selection. 您定义了userRange,但稍后您将遍历选择中的单元格。 Also you are using & incorrectly. 另外,您使用不正确。 You can try this: 您可以尝试以下方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM