根据 VBA 中的 If 语句插入箭头
Insert Arrow depending on If Statement in VBA
我正在尝试根据一个单元格编号是否大于另一个单元格编号来插入箭头。我的数据如下
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,7) 中的数字旁边插入一个黄色的横向箭头 ( C,8).
在此先感谢您的帮助。
如果您认为箭头出现在相邻单元格中而不是出现在单元格本身中,则可以在没有任何 VBA 的情况下执行此操作。
公式 =IF(C1>D1,"é",IF(C1<D1,"ê","è"))
将为您提供字体设置为 Wingdings.
的正确箭头
然后您可以使用条件格式来设置颜色。
接受答案后编辑:
这会将箭头添加到单元格中数字的末尾。
单元格的内容在更新后将被视为文本 - 对单元格执行任何数学函数(例如 SUM
)将 return #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
我正在尝试根据一个单元格编号是否大于另一个单元格编号来插入箭头。我的数据如下
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,7) 中的数字旁边插入一个黄色的横向箭头 ( C,8).
在此先感谢您的帮助。
如果您认为箭头出现在相邻单元格中而不是出现在单元格本身中,则可以在没有任何 VBA 的情况下执行此操作。
公式 =IF(C1>D1,"é",IF(C1<D1,"ê","è"))
将为您提供字体设置为 Wingdings.
的正确箭头
然后您可以使用条件格式来设置颜色。
接受答案后编辑:
这会将箭头添加到单元格中数字的末尾。
单元格的内容在更新后将被视为文本 - 对单元格执行任何数学函数(例如 SUM
)将 return #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