简体   繁体   English

如果第 7 列中的一个单元格等于另一个单元格中的值,则更改为另一个单元格中的值

[英]If a cell in Column 7 equals the value in another cell change to the value in another cell

The code below changes the value of any cell in column 7 whose value is equal to the value in AF14 and changes it to the value in AF15.下面的代码更改第 7 列中任何与 AF14 中的值相等的单元格的值,并将其更改为 AF15 中的值。 I would like to add more criteria say if is AF16 then change to A17, if is AF17 then change to AF18 and so on.我想添加更多标准,比如如果是 AF16 则更改为 A17,如果是 AF17 则更改为 AF18 等等。

Thanks in advance提前致谢

Private Sub macro13()

Dim i As Integer
Dim WK As Worksheet
Dim rg As Range

Set WK = Sheet4
For i = Cells(Rows.Count, 7).End(xlUp).Row To 1 Step -1
    If Cells(i, 7) = rg Then Cells(i, 7).Value = Range("AF15").Value
Next i

End Sub

Try something like this:尝试这样的事情:


Private Sub macro13()

Dim i As Integer
Dim WK As Worksheet
Dim rg As Range
Dim cell_arr(1 To 5, 1 To 2) As String

Set WK = ActiveSheet

' cell_arr(x,1) --> cell_arr(x,2)
cell_arr(1, 1) = "AF14": cell_arr(1, 2) = "AF15"
cell_arr(2, 1) = "AF16": cell_arr(2, 2) = "AF17"
cell_arr(3, 1) = "AF18": cell_arr(3, 2) = "AF19"
cell_arr(4, 1) = "AF20": cell_arr(4, 2) = "AF21"
cell_arr(5, 1) = "AF22": cell_arr(5, 2) = "AF23"

For k = 1 To UBound(cell_arr, 1)
    For i = Cells(Rows.Count, 7).End(xlUp).Row To 1 Step -1
    
        If Cells(i, 7) = Range(cell_arr(k, 1)) Then
                Cells(i, 7).Value = Range(cell_arr(k, 2)).Value
        End If
    
    Next i
Next k

End Sub

Your question is not entirely clear, I wonder two things:您的问题并不完全清楚,我想知道两件事:

  • Why do you do this in a macro?为什么要在宏中执行此操作? Do you want that value only to be filled in at a certain event (like a button-click)?您是否希望仅在某个事件(如单击按钮)时填写该值?
  • In case you don't need a macro, you might do this using either a bunch of nested IF(...) clauses, or you might opt for a combination of HLookup() (find a value somewhere inside a row) and OffSet(-1,0) (once you found a cell, take the one in the same row but in the column at the left).如果您不需要宏,您可以使用一组嵌套的IF(...)子句来执行此操作,或者您可以选择HLookup() (在行内某处查找值)和OffSet(-1,0)的组合OffSet(-1,0) (一旦你找到一个单元格,在同一行但在左边的列中取一个)。

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

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