【发布时间】:2015-03-02 20:26:52
【问题描述】:
这是场景:
如果选中了一个复选框 (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
【问题讨论】:
-
将
For Each cell In Sheet1.Range("SamePO")更改为For Each cell In Sheet1.Range("SamePO").Cells -
问题在于
cell.Name。如果“PO_Cmt”是一个命名范围,那么您将需要遍历 .Names 集合并与当前单元格进行比较。 -
尝试使用
cell.Name.Name -
Kyle 你能解释一下吗?
-
PO_Cmt 是我希望在遍历 Same_PO 范围中的每个单元格时跳过的命名范围。