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

时间:2017-10-19 09:50:36

标签: excel vba excel-vba 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列中的Cell值是否大于,小于或等于E列中的相应Cell值进行突出显示。 因此,我想在Cell(C,6)中的数字旁边插入一个绿色箭头,在Cell(C,7)中的数字旁边插入一个红色向下箭头,在Cell中的数字旁边插入一个黄色的侧面箭头( C,8)。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

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

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

接听回答后编辑:
这会将箭头添加到单元格中数字的末尾 更新后,单元格的内容将被视为文本 - 在单元格上执行任何数学函数(例如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  

我将查看条件格式规则.....

答案 1 :(得分:1)

同样的方法,您也可以尝试以下字符

Sub Up_arrow()
ActiveCell.FormulaR1C1 = "á"
With ActiveCell.Font
    .Name = "Wingdings"
    .ColorIndex = 10
    .TintAndShade = 0
End With
End Sub

Sub Down_arrow()
ActiveCell.FormulaR1C1 = "â"
With ActiveCell.Font
    .Name = "Wingdings"
    .ColorIndex = 46
    .TintAndShade = 0
End With
End Sub


Sub Right_arrow()
ActiveCell.FormulaR1C1 = "à"
With ActiveCell.Font
        .Name = "Wingdings"
        .ColorIndex = 40 ' or 22
        .TintAndShade = 0
End With
End Sub