If-Then-Else Statement

  • Thread starter loren.pottinger
  • Start date
L

loren.pottinger

It is not executing my If-Then-Else statement.
It seems the only statement being executed is: Cells(Row, Col).Value =
Range("K" & Row) and I only want it to do that if the condition is
true, and put a 0 in each cell in the range if it is false. Can someone

please look at my code and help me to get it to execute the
If-Then-Else statement. Thank you. Updated code as follows:

Sub Fill()


Dim TestDate As Date
Dim Col As Integer
Dim Row As Long


For Col = 13 To 29
For Row = 7 To 63


With TestDate = DateAdd("m", Range("F" & Row).Value, _
Range("H" & Row).Value)


If TestDate < Range("I" & Row).Value Then
Cells(Row, Col).Value = Range("K" & Row)
Range("F" & Row).Value = Range("F" & Row).Value - 1



Else
Cells(Row, Col).Value = 0
Range("F" & Row).Value = Range("F" & Row).Value - 1



End If
End With


Next Row
Next Col
End Sub
 
D

Dave Peterson

I'm not sure what's in those cells (I'd look there next), but the with statement
doesn't make much sense to me.

Option Explicit
Sub myFill()

Dim TestDate As Date
Dim myCol As Long
Dim myRow As Long

For myCol = 13 To 29
For myRow = 7 To 63
TestDate = DateAdd("m", Range("F" & myRow).Value, _
Range("H" & myRow).Value)

If TestDate < Range("I" & myRow).Value Then
Cells(myRow, myCol).Value = Range("K" & myRow)
Range("F" & myRow).Value = Range("F" & myRow).Value - 1
Else
Cells(myRow, myCol).Value = 0
Range("F" & myRow).Value = Range("F" & myRow).Value - 1
End If
End With
Next myRow
Next myCol

End Sub

And I'd stay away from using Row, Fill as variable/sub names. They are reserved
words in excel.
 
D

Dave Peterson

I deleted the With statment, but missed the "end with" statement. Remove that,
too.
 
L

loren.pottinger

Thanks Dave. It is still not reading my If-Then-Else. There are no 0s
in the cells where the condition is false. The information in the for
the DateAdd function is correct though. I'm certain that is not the
problem.
 
D

Dave Peterson

I'd add some debug.print statements to verify what you think is true:

debug.print testdate & vblf & Range("I" & myRow).Value

for example.

loren.pottinger said:
Thanks Dave. It is still not reading my If-Then-Else. There are no 0s
in the cells where the condition is false. The information in the for
the DateAdd function is correct though. I'm certain that is not the
problem.
 
L

loren.pottinger

Thanks dave, but where would I put that exactly? Would I put it under
this statement:

If TestDate < Range("I" & myRow).Value Then

If that's the case, I did and nothing happened, so does that mean it's
not doing what I want it to do? And do you have any thoughts on how I
may go about fixing it?
 
D

Dave Peterson

Put it before that line.

Then you'll know what is in those variables/cells before that line gets
executed.

loren.pottinger said:
Thanks dave, but where would I put that exactly? Would I put it under
this statement:

If TestDate < Range("I" & myRow).Value Then

If that's the case, I did and nothing happened, so does that mean it's
not doing what I want it to do? And do you have any thoughts on how I
may go about fixing it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top