如果语句跳过内部代码

时间:2018-05-21 14:57:29

标签: excel vba if-statement lookup-tables

首先,此代码属于NDA,我无法展示更多内容。 我试图让一个明星出现在黑色或黄色的纸张上。当代码超过工作表选择时,它会跳回主代码。如果那时我已经尝试了另一个和当前的Case格式。无论在单元格中的内容是什么,它都会跳过代码。我把数字,真实和虚假。整个程序基于查找表,我只是想为星星添加代码。任何帮助都会很棒。

  Sub Star()

  Range(Cells(nxtRow, "B"), Cells(nxtRow + 1, "B")).Select
  With Selection
    .MergeCells = True
  End With

   '8.3x11 sheet
    If (p_size = 1) Then
      y = 171.25 + 43.5 * Mtimes
        ActiveSheet.Shapes.AddShape(msoShape5pointStar, 23.25, y, 22, 
        _22).Select
     Select Case Cells(i, "s").Value

    'chooses yellow

        Case True
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 13
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

    'chooses black

        Case False
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 1
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

        Case Else: MsgBox ("star color indeterminate")
        End Select


'11x17 sheet
    ElseIf (p_size = 2) Then
     y = 160 + 33 * Mtimes
    ActiveSheet.Shapes.AddShape(msoShape5pointStar, 48#, y, 22, 22).Select

    Select Case Cells(i, "s").Value

    'chooses yellow

        Case True
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 13
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

    'chooses black

        Case False
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 1
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

        Case Else: MsgBox ("star color indeterminate")
        End Select

    End If

End Sub

最终的固定代码。在Wookiee的帮助下,我得到了正确的颜色。所以,我做了“我'收集的变量,并修复了跳过。我添加了工作表(' d"),通过查看正确的位置来正确地为星星着色。

  Sub Star(i)

  Range(Cells(nxtRow, "B"), Cells(nxtRow + 1, "B")).Select
With Selection
    .MergeCells = True
  End With

'8.3x11 sheet
  If (p_size = 1) Then
    y = 171.25 + 43.5 * Mtimes
    ActiveSheet.Shapes.AddShape(msoShape5pointStar, 23.25, y, 
22, 22).Select
    Select Case Sheets("d").Cells(i, "s").Value

    'chooses yellow

        Case True
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 13
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 
 255, 255)

    'chooses black

        Case False
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 0
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

        Case Else: MsgBox ("star color not found")
        End Select


'11x17 sheet
ElseIf (p_size = 2) Then
    y = 160 + 33 * Mtimes
    ActiveSheet.Shapes.AddShape(msoShape5pointStar, 48#, y, 
  22, 22).Select

    Select Case Sheets("d").Cells(i, "s").Value

    'chooses yellow

        Case True
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 13
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 
  255, 255)

    'chooses black

        Case False
            Selection.ShapeRange.Fill.Visible = msoTrue
            Selection.ShapeRange.Fill.Solid
            Selection.ShapeRange.Fill.ForeColor.SchemeColor = 0
            Selection.ShapeRange.Fill.Transparency = 0#
            Selection.ShapeRange.Line.Weight = 0.75
            Selection.ShapeRange.Line.DashStyle = msoLineSolid
            Selection.ShapeRange.Line.Style = msoLineSingle
            Selection.ShapeRange.Line.Transparency = 0#
            Selection.ShapeRange.Line.Visible = msoTrue
            Selection.ShapeRange.Line.ForeColor.SchemeColor = 64
            Selection.ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)

        Case Else: MsgBox ("star color not found")
        End Select
     Else: MsgBox ("paper size not found")
    End If

End Sub

1 个答案:

答案 0 :(得分:2)

检查" i"的价值你的两行变量:

Select Case Cells(i, "s").Value

它可能不在您期望的范围内。