amea6995
amea6995

Reputation: 37

If/ElseIf Statements only working for the first if statement

My If (ElseIf) statements seem to only for the first If statement. I'm not sure what I'm missing here. This is for a chart that needs boundaries that change based on the cell values C3:D11. The chart is essentially two overlapping graphs, which is why i have a secondary axes value. Both axes need to change during the IfElse section.

Sub Pyramid()

       Dim cht As Chart
       Set cht = Worksheets("AP-Chart").ChartObjects("Chart 4").Chart
       For Each cell In Range("C3:D11")


        If cell.Value < -15 Then
            cht.Axes(xlCategory).MinimumScale = -20
            cht.Axes(xlValue, xlSecondary).MinimumScale = -20
        ElseIf cell.Value > 15 Then
            cht.Axes(xlCategory).MaximumScale = 20
            cht.Axes(xlValue, xlSecondary).MaximumScale = 20
        ElseIf cell.Value < -20 Then
            cht.Axes(xlCategory).MinimumScale = -30
            cht.Axes(xlValue, xlSecondary).MinimumScale = -30
        ElseIf cell.Value > 20 Then
            cht.Axes(xlCategory).MaximumScale = 30
            cht.Axes(xlValue, xlSecondary).MaximumScale = 30
        ElseIf cell.Value < -30 Then
            cht.Axes(xlCategory).MinimumScale = -40
            cht.Axes(xlValue, xlSecondary).MinimumScale = -40
        ElseIf cell.Value > 30 Then
            cht.Axes(xlCategory).MaximumScale = 40
            cht.Axes(xlValue, xlSecondary).MaximumScale = 40
        ElseIf cell.Value < -40 Then
            cht.Axes(xlCategory).MinimumScale = -50
            cht.Axes(xlValue, xlSecondary).MinimumScale = -50
        ElseIf cell.Value > 40 Then
            cht.Axes(xlCategory).MaximumScale = 50
            cht.Axes(xlValue, xlSecondary).MaximumScale = 50
        End If
       Next cell

       ActiveSheet.ChartObjects("Chart 4").Activate
       ActiveChart.ChartArea.Select
       myPDF = "\\stchsfs\arboari$\Profile-Data\Desktop\Export Trial1\c1-" & Sheets("AP").Range("C" & i + 3).Value2 & ".pdf"
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=myPDF, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    i = i + 1
     Next counter

End Sub

Thanks!

Upvotes: 0

Views: 113

Answers (1)

M_Armendariz
M_Armendariz

Reputation: 150

It's simple, your if else statements are wrong.

-20 its lower than -15 so it always do the thing inside the first If.

You should do something like:

If cell.Value < -15 And cell.Value > -20 Then
    cht.Axes(xlCategory).MinimumScale = -20
    cht.Axes(xlValue, xlSecondary).MinimumScale = -20
ElseIf cell.Value < -20 And cell.Value > -30 Then
    //do stuff here

And you continue like that. You should do the same thing with the positive values, and I think you are not considering the values -20,-30, etc (because you are excluding them).

Upvotes: 2

Related Questions