匹配多个条件的代码无法处理不匹配/中断

默索

可以将操作分解为以下内容:

  1. 在工作表中,src2我使用复选框选择标准(可能有多个选择)
  2. 根据选定的标准,代码转到工作表src并查找匹配项
  3. 匹配项导出到另一个 Dst

除了在工作表中选择以下条件的情况外,代码工作正常src2

 - Criteria 1 - selected
 - Criteria 2 - not selected
 - Criteria 3 - selected

这意味着代码不能在选择之间留有空隙。它没有错误。只是什么都没有输出。

Dim rngSelectionTable As Range
Dim tempfolderpath As String
Dim Crit As String                                                          

Set rngSelectionTable = src2.Range("options_selectiontable")                

For temprow = 1 To rngSelectionTable.Rows.Count                             
    tempselected = rngSelectionTable(temprow, 2).Value   ' Checkbox value column
    Crit = rngSelectionTable(temprow, 5).Value           ' Criteria value column
    If tempselected = True Then                          ' If checkbox selected, then ...

            For Each r In src.Range("P4:P" & LastRow)    ' Analysis range in Sheet src
                If r <> 0 Then strValue = r              ' If cell in src is Non-Empty
                If strValue = Crit Then              ' If cell in Modules_List = Criteria
                    If CopyRange Is Nothing Then       ' If nothing copied before, then
                            Set CopyRange = r.EntireRow       ' Copy entire row
                    Else
                            Set CopyRange = Union(CopyRange, r.EntireRow) ' Else - add this row to previously copies
                    End If
                End If
            Next r
        End If
    Next temprow

    If Not CopyRange Is Nothing Then

    CopyRange.Copy
    Dst.Range("A324").Insert xlShiftDown     ' Starting cell for INSERTing the range
    End If
用户4039065

使联合范围到达目标工作表的最后一个操作失败。只要该范围由不连续的行组成,就不能在“插入复制的单元格”操作中使用它。在工作表上手动尝试它,您将看到该选项不可用。

您可以遍历联合范围的Range.Areas属性,并为每个连续范围(也称为Area)复制,插入复制的单元格。

Dim a As Range
If Not copyRange Is Nothing Then
    For Each a In copyRange.Areas
        'Debug.Print a.Address(0, 0)
        a.Copy
        dst.Range("A324").Insert xlShiftDown     ' Starting cell for INSERTing the range
    Next a
End If

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章