Excelmacros根据行中其他两列的值写入值

我有一个腌制,我希望得到一些帮助。 有一天我按照这个逻辑拼凑了一个macros:

如果B列中的某个单元格具有特定的文本“brand1”,并且该行在列U中的值为“y”,则使用文本string“sample1”填充该行的列W。

我前几天有这个工作,但由于某种原因,今天不工作。 任何想法或想法? 完全开放这个macros的想法,反正使用不同的macros。

Sub PromoID() Application.ScreenUpdating = False Dim lastRow As Long Dim cell As Range lastRow = Range("B" & Rows.Count).End(xlUp).Row For Each cell In Range("B2:B" & lastRow) If InStr(1, cell.Value, "Brand1") <> 0 Then InStr(1, cell.Offset(, 21).Value, "y") <> 0 Then cell.Offset(, 23).Value = "sample1" End If End If Next Application.ScreenUpdating = True End Sub 

你离这个答案还差得很远。 保持你的眼睛去除轻微的错误。

 Sub PromoID() Application.ScreenUpdating = False Dim lastRow As Long Dim cell As Range lastRow = Range("B" & Rows.Count).End(xlUp).Row + 1 For Each cell In Range("B2:B" & lastRow) If InStr(1, cell.Value, "Brand1") <> 0 Then If InStr(1, cell.Offset(0, 19).Value, "y") <> 0 Then cell.Offset(0, 21).Value = "sample1" End If End If Next Application.ScreenUpdating = True End Sub 

输出:

在这里输入图像说明

尽量使一切大写。 用Ucase()

 If ucase(InStr(1, cell.Value, "Brand1") ) <> 0 Then ucase(InStr(1, cell.Offset(, 21).Value, "y") ) <> 0 Then cell.Offset(, 23).Value = "sample1"