mercredi 16 juin 2021

Using loop to less the uses of if formula

Good Day!

I am writing a code in which I have 03 cell in which user will input yes or no. After submitting the answer the user will get a result in another cell. Like if his answer is yes-yes-yes then the result will be 400-400-400 and if his answer is yes-no-yes then the result will be 400-0-400 and if his answer is yes-no-no then the result will be 400-0-0 and like this so on.

I have written a code

If Cells(Number, 8).Value = "yes" And Cells(Number, 9).Value = "yes" And Cells(Number, 10).Value = "no" Then
        Range(planningRange).Value = Round(Cells(Number, 18), 0) & "-" & Round(Cells(Number, 18), 0) & "-" & "0"
    ElseIf Cells(Number, 8).Value = "yes" And Cells(Number, 10).Value = "no" And Cells(Number, 9).Value = "yes" Then
        Range(planningRange).Value = Round(Cells(Number, 18), 0) & "-" & "0" & "-" & Round(Cells(Number, 18), 0)
    ElseIf Cells(Number, 9).Value = "no" And Cells(Number, 10).Value = "yes" And Cells(Number, 8).Value = "yes" Then
        Range(planningRange).Value = "0" & "-" & Round(Cells(Number, 18), 0) & "-" & Round(Cells(Number, 18), 0)
    ElseIf Cells(Number, 8).Value = "yes" And Cells(Number, 9).Value = "yes" And Cells(Number, 10).Value = "yes" Then
        Range(planningRange).Value = Round(Cells(Number, 18), 0) & "-" & Round(Cells(Number, 18), 0) & "-" & Round(Cells(Number, 18), 0)
    Else
        Range(planningRange).Value = "-"
    End If

The good news is the code work perfectly! But the bad news is this is not the proper way of it. Because this is well if the input cell is 03 or 04. But if my input cell is going to be 11. So, if I want this probably I will have to write around 243 if formula. But that is not possible. So, can anyone suggest any kind of loop or anything else which can make this easy?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire