2015-03-02 39 views
0

这是情形:For循环通过命名范围细胞名

如果一个复选框(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 
+0

变化'为每个小区在Sheet1.Range(“SamePO”) '为'对于Sheet1.Range中的每个单元格(“SamePO”)。Cells' – chancea 2015-03-02 20:28:03

+0

问题出在'cell.Name'上。如果“PO_Cmt”是一个已命名的范围,那么您将需要遍历.Names集合并与当前单元格进行比较。 – Kyle 2015-03-02 20:39:27

+0

尝试使用'cell.Name.Name' – 2015-03-02 22:24:18

回答

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 
+0

这太棒了!谢谢! – James 2015-03-04 20:00:31