简体   繁体   English

Excel-如果行包含特定值,则重命名单元格

[英]Excel - Rename cell if row contains certain value

I've seen some similar questions but can't quite find an answer for my situation. 我已经看到了一些类似的问题,但是找不到适合我情况的答案。 If data is found in a row, then Column C of that row will be labeled with that data. 如果在一行中找到数据,则该行的C列将用该数据标记。 But I also want all columns below this row to contain this same label in Column C. Until the next data is found, then I want to repeat the same process for the whole document until every row had data in Column C reflecting the data from its particular section. 但是我还希望该行下方的所有列在C列中都包含相同的标签。直到找到下一个数据,然后我要对整个文档重复相同的过程,直到每一行在C列中都有反映其数据的数据为止。特定部分。

I probably did a poor job of explaining that, so here is what I would like to happen. 我可能在解释这一点上做得很差,所以这就是我想发生的事情。 Any help would be greatly appreciated. 任何帮助将不胜感激。

[Example of what I want the outcome to be.] [我希望结果如何的示例。]

This is very simple with just IF formula. 仅使用IF公式,这非常简单。 Here is the pseudocode: 这是伪代码:

if the sentence start with "class" then 
  refer to cell next to him
else:
  refer to cell on top of him

公式视图

Which in code look like: 在代码中如下所示:

Sub test()
    Dim rng As Range
    With ActiveSheet
        For Each rng In Intersect(.Columns("C"), .UsedRange)
            If Left$(Trim(rng.Offset(, 1)), 5) = "Class" Then
                rng = rng.Offset(, 1)
            Else
                rng = rng.Offset(-1)
            End If
        Next rng
    End With
End Sub

If you were to use column D to determine the last row to loop to you might use the following (swop the "D" for a different column letter if you want to use a different column to determine how far to loop to get to last row) 如果要使用D列来确定要循环到的最后一行,则可以使用以下命令(如果要使用其他列来确定要循环到最后一行的距离,请在不同的列字母上加上“ D” )

Option Explicit

Sub test()
    Dim rng As Range
    With ActiveSheet
        For Each rng In .Range(.Cells(1, "C"), .Cells(.Cells(.Rows.Count, "D").End(xlUp).row, "C"))
            If Left$(Trim(rng.Offset(, 1)), 5) = "Class" Then
                rng = rng.Offset(, 1)
            Else
                rng = rng.Offset(-1)
            End If
        Next rng
    End With
End Sub

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

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