错误检查应该去哪里?

时间:2015-09-04 15:55:04

标签: excel excel-vba vba

所以这可能是一个非常愚蠢的问题,但我正在梳理这个代码,我需要检查Cell(A5,A7,A10)是否为空我想要弹出一个消息框,说明这些字段必须是在继续之前填补。这是整个代码块。我只是无法弄清楚错误检查的位置。任何帮助都会很精彩。谢谢!

'Set Variables
Products = Range("B1") + 3 'Number of Products on Sheet
Count = 4 'Count Used to Change the Active Cell

Do While Count <= Products
    'Checks if Product is Add On Only
    If Cells(Count, 17) = "yes" Then
        If Cells(Count, 5) = "grouped" Or Cells(Count, 5) = "configurable" Then
            'Adds the **ADD ON ONLY** and Combines the Cells to Create Main Title
            Cells(Count, 7) = Trim("** ADD ON ONLY** " & Cells(Count, 2) & " " & Cells(Count, 3) & " " & Cells(Count, 6))
            'Copies Main Title To Short Description & Image Labels
            Cells(Count, 37) = Cells(Count, 7)
            'Checks if product is Child Configurable
            If Cells(Count, 4) <> "simple - child config" Then
                'Adds Main Title to Image Labels when Product is not Child Config
                Cells(Count, 56) = Cells(Count, 7)
                Cells(Count, 58) = Cells(Count, 7)
                Cells(Count, 60) = Cells(Count, 7)
            End If

         Else
            'Adds the **ADD ON ONLY** and Combines the Cells to Create Main Title
            Cells(Count, 7) = Trim("** ADD ON ONLY** " & Cells(Count, 2) & " " & Cells(Count, 3) & " " & Cells(Count, 6) & " " & Cells(Count, 24))
            'Copies Main Title To Short Description & Image Labels
            Cells(Count, 37) = Cells(Count, 7)
            'Checks if product is Child Configurable
            If Cells(Count, 5) <> "simple - child config" Then
                'Adds Main Title to Image Labels when Product is not Child Config
                Cells(Count, 56) = Cells(Count, 7)
                Cells(Count, 58) = Cells(Count, 7)
                Cells(Count, 60) = Cells(Count, 7)
            End If
        End If

    'If Product Is NOT Add On Only
    Else
        If Cells(Count, 5) = "grouped" Or Cells(Count, 5) = "configurable" Then
             'Adds Combines the Cells to Create Main Title
            Cells(Count, 7) = Trim(Cells(Count, 2) & " " & Cells(Count, 3) & " " & Cells(Count, 6))
            'Copies Main Title To Short Description & Image Labels
            Cells(Count, 37) = Cells(Count, 7)
             'Checks if product is Child Configurable
            If Cells(Count, 5) <> "simple - child config" Then
                'Adds Main Title to Image Labels when Product is not Child Config
                Cells(Count, 56) = Cells(Count, 7)
                Cells(Count, 58) = Cells(Count, 7)
                Cells(Count, 60) = Cells(Count, 7)
            End If
        Else
            'Adds Combines the Cells to Create Main Title
            Cells(Count, 7) = Trim(Cells(Count, 2) & " " & Cells(Count, 3) & " " & Cells(Count, 6) & " " & Cells(Count, 24))
            'Copies Main Title To Short Description & Image Labels
            Cells(Count, 37) = Cells(Count, 7)
             'Checks if product is Child Configurable
            If Cells(Count, 5) <> "simple - child config" Then
                'Adds Main Title to Image Labels when Product is not Child Config
                Cells(Count, 56) = Cells(Count, 7)
                Cells(Count, 58) = Cells(Count, 7)
                Cells(Count, 60) = Cells(Count, 7)
            End If
        End If
    End If
    'Performs Actions on Next Cell Range

     Count = Count + 1

Loop
End Sub

1 个答案:

答案 0 :(得分:1)

  

单元格(A5,A7,A10)是空的我想弹出一个消息框,说明在继续之前必须填写这些字段。

在这种情况下,我会在顶部加起来。

If IsEmpty(Range("A5").Value) And IsEmpty(Range("A7").Value) And IsEmpty(Range("A10").Value) Then
    MsgBox ("Please fill in A5, A7, and A10 - then rerun the macro.")
    Exit Sub
    End If