vba - 根据 VBA 中的 If 语句插入箭头

标签 vba excel if-statement conditional-formatting

我正在尝试根据一个单元格编号是否大于另一个来插入箭头。我掌握的数据如下。

     C         E

6  20800     20400
7  5038       6003
8  46325     46325

我目前拥有的 if 语句如下,我想补充一下。
For lngI = 1 To 3
If Worksheets("Front End").Cells(lngI + 5, 3).Value > Worksheets("Front End").Cells(lngI + 5, 5).Value Then
    Worksheets("Front End").Cells(lngI + 5, 3).Interior.ColorIndex = 40
ElseIf Worksheets("Front End").Cells(lngI + 5, 3).Value < Worksheets("Front End").Cells(lngI + 5, 5).Value Then
    Worksheets("Front End").Cells(lngI + 5, 3).Interior.ColorIndex = 35
ElseIf Worksheets("Front End").Cells(lngI + 5, 3) = Worksheets("Front End").Cells(lngI + 5, 5) Then
    Worksheets("Front End").Cells(lngI + 5, 3).Interior.ColorIndex = 2
End If
Next lngI

所以目前我所做的只是根据 C 列中的单元格值是否大于、小于或等于 E 列中相应的单元格值来突出显示。
因此,我想在单元格 (C,6) 中的数字旁边插入一个向上指向的绿色箭头,在单元格 (C,7) 中的数字旁边插入一个红色向下箭头,在单元格中的数字旁边插入一个黄色侧向箭头 ( C,8)。

提前感谢您的帮助。

最佳答案

如果您对出现在相邻单元格中而不是在单元格本身中的箭头表示满意,则可以在没有任何 VBA 的情况下执行此操作。

公式=IF(C1>D1,"é",IF(C1<D1,"ê","è"))会给你正确的箭头,字体设置为 翅膀 .
然后,您可以使用条件格式来设置颜色。

接受答案后编辑:
这会将箭头添加到单元格中数字的末尾。
更新后单元格的内容将被视为文本 - 对单元格执行任何数学函数(例如 SUM )将返回 #VALUE 错误。

Sub Test()

    Dim lngI As Long
    Dim CharToAdd As String
    Dim ColourToUse As Long

    For lngI = 1 To 3

        With Worksheets("Front End")
            If .Cells(lngI, 3) > .Cells(lngI, 4) Then
                CharToAdd = " é"        'Up arrow.
                ColourToUse = -11489280 'Green.
            ElseIf .Cells(lngI, 3) < .Cells(lngI, 4) Then
                CharToAdd = " ê"        'Down arrow.
                ColourToUse = -16776961 'Red.
            Else
                CharToAdd = " è"        'Right arrow.
                ColourToUse = -16711681 'Yellow.
            End If

            'Add the character to the end of the number.
            'Note - cell is now a text string.
            .Cells(lngI, 3) = .Cells(lngI, 3) & CharToAdd

            'Format last character in cell.
            With .Cells(lngI, 3).Characters(Start:=Len(.Cells(lngI, 3)), Length:=1).Font
                .Name = "Wingdings"
                .Color = ColourToUse
            End With
        End With

    Next lngI

End Sub

如果您希望箭头出现在单元格的开头:
Sub Test()

    Dim lngI As Long
    Dim CharToAdd As String
    Dim ColourToUse As Long

    For lngI = 1 To 3

        With Worksheets("Front End")
            If .Cells(lngI, 3) > .Cells(lngI, 4) Then
                CharToAdd = "é "        'Up arrow.
                ColourToUse = -11489280 'Green.
            ElseIf .Cells(lngI, 3) < .Cells(lngI, 4) Then
                CharToAdd = "ê "        'Down arrow.
                ColourToUse = -16776961 'Red.
            Else
                CharToAdd = "è "        'Right arrow.
                ColourToUse = -16711681 'Yellow.
            End If

            'Add the character to the start of the number.
            'Note - cell is now a text string.
            .Cells(lngI, 3) = CharToAdd & .Cells(lngI, 3)

            'Format first character in cell.
            With .Cells(lngI, 3).Characters(Start:=1, Length:=1).Font
                .Name = "Wingdings"
                .Color = ColourToUse
            End With
        End With

    Next lngI

End Sub  

我将研究条件格式规则......

关于vba - 根据 VBA 中的 If 语句插入箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46827163/

相关文章:

excel - 使用搜索和循环从一张纸复制并粘贴到另一张纸

VBA-将每个工作表保存为Excel工作簿中的值

vba - 如何解决VBA中的 'Next without For'错误

wpf - VSTO WPF ContextMenu.MenuItem 单击未引发的 TaskPane 外部

java - 从 Servlet 下载 Excel

python - Jinja2 中关于 for 语句的 If 语句

java - Setter - 检查值

VBA 无效使用 null

C#通过OleDb读取打开的Excel文件

java - 两个小型结构的性能差异