For Loop through Named Range单元格名称

时间:2015-03-02 20:26:52

标签: excel excel-vba vba

这是情景:

如果选中了复选框(SamePObx),我想遍历命名范围并验证单元格中是否存在至少一个值,但包含值的单元格不能命名为“PO_Cmt”。如果所有单元格都是空白,则会弹出一个msgbox。

    If Sheet1.SamePObx.Value = True Then 'if checkbox is selected
        For Each cell In Sheet1.Range("SamePO")
            'if the cell is blank and isn't name PO_Cmt
            If (cell.Value <> "") And (cell.Name <> "PO_Cmt") Then
                x = x + 1 'one PO is present
                Exit For
            End If
        Next cell

        'if no POs present, flag
         If x = 0 Then
             MsgBox "Please provide the necessary PO#(s)"
             GoTo cont
     End If

我遇到的问题是1004的运行时错误。这一行是问题所在:

If (cell.Value <> "") And (cell.Name <> "PO_Cmt") Then

1 个答案:

答案 0 :(得分:1)

If Sheet1.SamePObx.Value = True Then 'if checkbox is selected

    x = Application.CountA(Sheet1.Range("SamePO"))

    If Range("PO_Cmt").Value <> "" Then x = x - 1

    'if no POs present, flag
     If x = 0 Then
         MsgBox "Please provide the necessary PO#(s)"
         GoTo cont
     End If
 End If