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

时间:2015-02-19 18:00:46

标签: excel vba excel-vba

我有一个泡菜,我希望得到一些帮助。前几天,我按照这个逻辑拼凑了一个宏:

如果列B中的单元格具有特定文本“brand1”,并且该行在列U中具有值“y”,则使用文本字符串“sample1”填充该行的列W.

我前几天工作了,但由于某种原因,它今天不起作用。任何想法或想法?完全接受废弃这个宏的想法,并使用不同的宏。

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

2 个答案:

答案 0 :(得分:0)

尝试将所有内容都设为大写。使用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"

答案 1 :(得分:0)

你的回答并不是那么远。保持你的眼睛去除轻微的错误。

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

输出:

enter image description here

相关问题