按照VBA中的If语句插入箭头

我试图根据一个单元格号是否大于另一个插入一个箭头。 我拥有的数据如下。

CE 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,"ê","è"))会给你正确的箭头,字体设置为Wingdings
然后,您可以使用条件格式设置颜色。

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

我会研究条件格式规则…..

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

 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