vendredi 23 mars 2018

VBA If Statement keeps running through ifs

This if statement is supposed to be looking at a cell and performing an action based on what's in that cell. If I just keep the first line of the statement, the code works and it only does what I want it to do for that situation. However, if I add multiple "tasks" after the "Then", it just runs all the way through the whole program. I know I'm missing some syntax here that will make it do "If [this] Then [this, this and this] ElseIf [this] Then [this this and this].

Making the second-sixth "If"'s into "ElseIf" gives me the error "ElseIf without block If"

Let me know what I'm missing, I'm guessing some iteration of For, Next etc. but I'm not familiar with that function.

Sub trythis()

Dim tol As String
Dim formblah As String


tol = Range("I7").Value
formblah = "=IF(D21>C21+" & tol & ",""FAIL"",IF(D21<C21+" & tol & ",""PASS"",IF(D21=C21+" & tol & ",""PASS-BONUS"",""N/A"")))"

If Sheets("Caliper").Range("C5").Value = 1 Then Rows("21:26").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    Range("E21:E26").Value = formblah
    Range("C21") = 1
    Range("C21").Select
    Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
    Step:=1, Stop:=6, Trend:=False




ElseIf Sheets("Caliper").Range("C5").Value = 2 Then Rows("21:28").Insert 
Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrBelow
    Range("E21:E28").Value = formblah
    Range("C21") = 1
    Range("C21").Select
    Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
    Step:=1, Stop:=8, Trend:=False

Aucun commentaire:

Enregistrer un commentaire