【问题标题】:For Loop through Named Range cells namesFor 循环遍历命名范围单元格名称
【发布时间】: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 范围中的每个单元格时跳过的命名范围。

标签: excel vba


【解决方案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

【讨论】:

  • 这太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-12-21
  • 2015-01-07
  • 1970-01-01
  • 2018-01-09
  • 2017-07-05
  • 2018-05-11
  • 2020-01-30
  • 1970-01-01
相关资源
最近更新 更多