Search code examples
excelvbaautocad

Is there a better place for the IF statement in the for loop


For k = 0 To (.Cells(38, 4).Value - 1)
    For i = 70 To 73
        For j = 0 To 2
            If j = 0 Then
                bumpvalue = k * (.Cells(39, 4).Value + .Cells(31, 4).Value)
            Else
                bumpvalue = 0
            End If
            StartPoint(j) = .Cells(i, 8 + j).Value + bumpvalue
            EndPoint(j) = .Cells(i + 1, 8 + j).Value + bumpvalue
        Next j

        Set lineObj = DWGFile.ModelSpace.AddLine(StartPoint, EndPoint)
    Next i
Next k

Is there a better place or way to implement the "bumpvalue" which only applies when j = 0?

something tells me below does not fly.

 StartPoint(j) = .Cells(i, 8 + j).Value + if j = 0 then k * (.Cells(39, 4).Value + .Cells(31, 4).Value) else 0

Solution

  • Your idea was very close. Try the IIf statement instead:

    StartPoint(j) = .Cells(i, 8 + j).Value + IIf(j = 0, k * (.Cells(39, 4).Value + .Cells(31, 4).Value), 0)