user1977802
user1977802

Reputation: 353

VBA If, elseif statement, how to set a constant when the first IF statement is not met

I have a If, Elseif, Else statement. If the first If is met I would like to plug in calculated values for runoff and percolation. However, if the first IF condition is not met, runoff and percolation will always get assigned 0's. I have pasted my code below. The problem I am having is that if I set percolation and runoff=0 after the first IF statement, my print sub will always print 0's, not the calculated values. How can I assign runoff and percolation as constants if the first IF condition is not met? I know I can just assign the runoff=0, percolation=0 in each subsequent condition, but I am trying to reduce the bulkiness of my code.

 Do
         If (fc - WC(j - 1) + RefET(i)) <= Precip(i) Then
                WC(j) = fc
                WCinit(j) = WC(j)
                Runoff(j - 1) = (Precip(i) - (fc - WC(j - 1) + RefET(i))) * 0.5
                Percolation(j - 1) = (Precip(i) - (fc - WC(j - 1) + RefET(i))) * 0.5
'    Runoff(j - 1) = 0 <-If I assign these here, it "overrides" the value I calculated on the previous line. I want to reduce the bulkiness of my code by assigning these values if the first IF condition is not met
'    Percolation(j - 1) = 0
          ElseIf (WC(j - 1) + Precip(i) - RefET(i)) > pwp Then
                 WC(j) = WC(j - 1) + Precip(i) - RefET(i)
                 WCinit(j) = WC(j)
                 Runoff(j - 1) = 0
                 Percolation(j - 1) = 0
        Else
                WC(j) = pwp
                WCinit(j) = WC(j)
                Runoff(j - 1) = 0
                Percolation(j - 1) = 0
        End If
                j = j + 1
                i = i + 1
                Loop While j < 14

Upvotes: 0

Views: 885

Answers (1)

BPCS
BPCS

Reputation: 56

Or when you dimention them. Dim Percolation as int = 0

Upvotes: 1

Related Questions