hiko-blog

VBA業務改善

MENU

条件 振り分け

Sub UpdateDColumn()
    Dim lastRow As Long
    Dim i As Long
    
    ' 最終行を取得
    lastRow = Cells(Rows.Count, "C").End(xlUp).Row
    
    ' ループして条件に基づいてD列を更新
    For i = 2 To lastRow
        If InStr(1, Cells(i, "C").Value, "AAA") > 0 And InStr(1, Cells(i, "C").Value, "Z") > 0 Then
            Cells(i, "D").Value = "A111"
        ElseIf InStr(1, Cells(i, "C").Value, "BBB") > 0 And InStr(1, Cells(i, "C").Value, "X") > 0 Then
            Cells(i, "D").Value = "B222"
        ElseIf InStr(1, Cells(i, "C").Value, "CCC") > 0 Then
            Cells(i, "D").Value = "C333"
        Else
            Cells(i, "D").Value = "ZZZZ"
        End If
    Next i
End Sub