【问题标题】:If statement skipping internal codeIf 语句跳过内部代码
【发布时间】:2018-05-21 14:57:29
【问题描述】:

首先,此代码受 NDA 保护,我无法展示更多内容。 我试图让星星在一张纸上显示为黑色或黄色。当代码通过工作表选择时,它会跳回主代码。我尝试了另一种 if then 和当前的 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 的帮助下,我得到了正确的颜色。因此,我将“i”设置为一个可收集的变量,并修复了跳过问题。我添加了工作表 ('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

【问题讨论】:

  • Select Case Cells(i, "s").Value中定义的“i”在哪里
  • 在前面的操作中。它是最多 20 项的项目列表中的数字。每次我运行它时都显示得很好,并且查找将 False 填充为 105。当我在 Select 案例中休息时运行它时,它永远不会停止。
  • 哎呀,对不起,我又跑了一次,我是空的。我是一个新程序员。谢谢。
  • 我会将其作为答案发布,以便您标记它。很高兴我能帮上忙。
  • 谢谢比尔。我不知道如何让它成为现在有效的答案。

标签: excel vba if-statement lookup-tables


【解决方案1】:

检查两行中“i”变量的值:

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

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

【讨论】:

  • 最终固定代码。在 Wookiee 的帮助下,我得到了正确的颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多