简体   繁体   English

VBA:根据单元格中的特定值格式化和更改单元格值

[英]VBA: Format and change cell value based on specific values in cell

I would like the below to change the value of the particular cell to "Rank" if two consecutive cells in the column are "#Name"如果列中的两个连续单元格是“#Name”,我希望下面将特定单元格的值更改为“Rank”

In addition, it would be helpful if the formatting of the cell could be red and font color white, while the value in the preceding cell would be nothing, and color blue另外,如果单元格的格式可以是红色,字体颜色可以是白色,而前面的单元格中的值可以是空的,颜色是蓝色的,那会很有帮助

This is what I wrote but it isn't working, really appreciate guidance-这是我写的,但它不起作用,非常感谢指导-

Sub FormatRankColmn()
'
' FormatRankColmn Macro
'

'
Dim cell As Range

For Each cell In Range("D1:D250").SpecialCells(xlCellTypeConstants)

If cell.Offset(-1, 0).Value = "#NAME" Then
    If cell.Value = "#Name" Then
        Range(cell).Value = "Rank"
    End If
End If
Next
End Sub

You need to check for cell in first row, otherwise this raise an error (no cell in row 0): cell.Offset(-1, 0)您需要检查第一行中的单元格,否则会引发错误(第 0 行中没有单元格): cell.Offset(-1, 0)

I believe this is what you are after:我相信这就是你所追求的:

Sub FormatRankColmn()

Dim cell As Range

For Each cell In Range("D1:D250").SpecialCells(xlCellTypeConstants)

If cell.Value = "#NAME" And cell.Row <> 1 Then
    If cell.Offset(-1, 0).Value = "#NAME" Then
        cell.Value = "Rank"
    End If
End If
Next
End Sub

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

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