【问题标题】:If/ElseIf Statements only working for the first if statementIf/ElseIf 语句仅适用于第一个 if 语句
【发布时间】:2018-10-01 12:12:09
【问题描述】:

我的 If (ElseIf) 语句似乎只针对第一个 If 语句。我不确定我在这里缺少什么。这适用于需要根据单元格值 C3:D11 更改边界的图表。该图表本质上是两个重叠的图表,这就是为什么我有一个辅助轴值。在 IfElse 部分中,两个轴都需要更改。

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

谢谢!

【问题讨论】:

  • 考虑Select Case,您可以在其中使用Case -20 to -15 样式语句来描述范围。 if ... elseif 语句可能会令人困惑且难以维护(正如您刚刚发现的那样!)

标签: vba loops if-statement charts


【解决方案1】:

很简单,你的 if else 语句是错误的。

-20 比 -15 低,所以它总是在第一个 If 内做事。

你应该这样做:

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

然后你就这样继续下去。您应该对正值做同样的事情,我认为您没有考虑值 -20、-30 等(因为您将它们排除在外)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多