Search code examples
vbaexcelsetvalue

VBA - Change Cell Value based on two other cells


I need this script to empty 4 cells, and change a cell from Done to Pending when both conditions are met.

I am still very inexperienced in VBA so apologies for the very basic and ugly code :P

 If Range("C5").Value Like "Done" And Range("D5").Value = "1" Then
     Range("A5:B5").ClearContents
     If Range("C5").Value Like "Done" And Range("D5").Value = "1" Then
         Range("D5:E5").ClearContents

         If Range("C5").Value Like "Done" And Range("D5").Value = "1" Then
             Range("C5").Value = "Pending"

         End If
     End If
 End If

I don't get why the C5 Cell doesn't change itself to Pending in this situation, since the D5 Cell does clear itself.

Could it be because I use Data Validation for the cell C5? It fetches a list named =TaskState

TaskState consists of the following 4 choices: Pending In Progress On Hold Done

Many Thanks


Solution

  • Try this:

     If Range("C5").Value Like "Done" And Range("D5").Value = "1" Then
        Range("A5:B5").ClearContents
        Range("D5:E5").ClearContents
        Range("C5").Value = "Pending"
    
     End If
    

    The issue is as soon as you clear the contents on Column D it is no longer = 1 and the third If returns false. So it never fires on the third if statement.

    The other if statements are not needed if they are testing the same thing.