循环有多个约束

时间:2014-07-03 09:00:01

标签: excel vba loops excel-vba nested-loops

我目前有一个查找列表的宏(例如下面的例子),目前只有在问题失败时才会撤回评论。见代码。我需要做的是构建一个简单的方法来撤回评论,如果它是一个通行证并且有评论。如果它是没有评论的通行证则需要忽略。

我有一些想法,但看起来过于复杂。

代码;

For Each Cell In Sheets("Sharepoint Raw").Range("M2:M3000")
          If Cell.Value = mpan Then
                For Each x In Sheets("Sharepoint Raw").Range("O" & Cell.row & ":AG" & Cell.row & "")
                    If x.Value = "Fail" Then
                        copycount = copycount + 1
                        Sheets(1).Cells(copycount, 2) = Sheets("Sharepoint Raw").Cells(1, x.Column)
                        Sheets(1).Cells(copycount, 4) = "Fail"
                        Sheets(1).Cells(copycount, 5) = Sheets("Sharepoint Raw").Cells(x.row, x.Column + 1)
                        Sheets(1).Cells(305, 16) = Sheets("Sharepoint Raw").Cells(x.row, 37)
                    Else
                        Sheets(1).Cells(305, 16) = Sheets("Sharepoint Raw").Cells(x.row, 37)
                    End If
                Next x
       End If
    Next Cell

烘焙桌;

╔══════╦══════════════╦══════╦═══════════════╦══════╦═══════════════╦══════╦══════════════════╗
║ Q1.  ║ Q1. Comments ║ Q2.  ║ Q2. Comments? ║ Q3.  ║ Q3. Comments? ║ Q4.  ║  Q4. Comments?   ║
╠══════╬══════════════╬══════╬═══════════════╬══════╬═══════════════╬══════╬══════════════════╣
║ Fail ║              ║ Fail ║               ║ Pass ║               ║ Pass ║                  ║
║ N/A  ║              ║ N/A  ║               ║ Pass ║               ║ Pass ║ Example comment' ║
║ Pass ║              ║ Fail ║               ║ Pass ║               ║ Pass ║                  ║
╚══════╩══════════════╩══════╩═══════════════╩══════╩═══════════════╩══════╩══════════════════╝

需要类似的东西(显然这不会起作用但是有类似的东西)

If x.Value = "Pass" AND X+1.value IS NOT NULL Then

1 个答案:

答案 0 :(得分:0)

考虑下面的宏:

Option Explicit
Sub Offset()

  Dim Cell As Range

  Set Cell = Range("D27")

  Debug.Print Cell.Address
  Debug.Print Cell.Offset(0, 1).Address
  Debug.Print Cell.Offset(5, -3).Address

End Sub

它将以下内容输出到立即窗口:

$D$27
$E$27
$A$32

我已将Cell设置为任意单元格地址。 Debug.Print Cell.Address按预期提供输出$D$27

Cell.Offset(0, 1)在列号中加1,结果为输出$E$27。这是您寻求的X+1

最后一个示例与您的要求没有直接关系,但表明您还可以调整行号并允许负偏移。