Search code examples
excelvba

Write text if 3 criteria is met VBA


I'm wondering if I could write "text" in column D if this criteria are met:

  1. Column A is not null
  2. Column B contains "yes"
  3. Column C is not null

These are my current idea for the criteria. But, I have no idea of how to do the rests.

Sheets(sourceSheet).Range("B2:AG" & lastRowSourceSheet).AutoFilter field:=3, Criteria1:="<>"
Sheets(sourceSheet).Range("B2:AG" & lastRowSourceSheet).AutoFilter field:=29, Criteria1:="TIDAK DISARANKAN"
Sheets(sourceSheet).Range("B2:AG" & lastRowSourceSheet).AutoFilter field:=31, Criteria1:="<>"

Please create the code in dynamic way because I have thousands of data rows. Any suggestion of how I could write the code is appreciated. I hope it make sense. Thank you so much!


Solution

  • Sub test()
    Dim LastRow As Long
    Dim i As Long
    
    LastRow = 7 'you need to calculate this depending on your dataset
    
    For i = 1 To LastRow 'from row 1 to 7
        If Len(Range("A" & i).Value) > 0 _
        And Len(Range("C" & i).Value) > 0 _
        And InStr(1, Range("B" & i).Value, "yes") > 0 Then
            Range("D" & i).Value = "some text"
        End If
    Next i
    
    End Sub
    

    enter image description here