首页 > 解决方案 > 跨工作表的选择性单元格链接自动化 > 运行时错误 91

问题描述

我正在寻找自动将多张纸中的数据选择性复制和堆叠到一张纸中。更具体地说,我有 4 列(M、H、A 和 F),我需要根据 IEg 列的同一行值有选择地复制单元格,具体情况如下:

Worksheets 2...N  
Column A_____Column F_____Column H_____Column I_____Column M  
_#####________AAAAAA______AAAAAA_______Rqrmnt_______Date
_#####________AAAAAA______AAAAAA_______Heading_______Blank

对于 N 个工作表中 I = Rqrmnt 列的所有行,我需要将 A、F、H 和 M 列中的相应值复制到工作表 1 中,从上到下堆叠每个工作表的导入,例如:

Worksheet 2 Column A...Worksheet 2 Column M  
Worksheet 3 Column A...Worksheet 3 Column M  
... 
Worksheet N Column A...Worksheet N Column M  

我需要能够对结果表执行有限的操作,特别是按列 M 的值对行进行排序

由于我有数百个这样的条目,我不希望通过一一链接单元格来构建它。此外,我更愿意单独放置复制的伪列(即在主电子表格上以 M>H>A>F 的顺序重新排列它们)。我有以下宏,源自这些 帖子(感谢 urdearboy 在下面对第二个链接帖子的评论)。但是,当我尝试运行宏时出现运行时错误 91 错误,并且调试器会突出显示下面标识的行。虽然这篇文章解释了错误本身,但我并没有帮助我解决这个问题。我尝试将 sourceSheetLastRow 初始化为任意数字,并Set在公式前面加上关键字,但无济于事。

Option Explicit

Sub Test()

    Dim summarySheetTargetRow As Long
    Dim sourceSheetTargetRow As Long
    Dim sourceSheetLastRow As Long
    Dim sourceSheetIndex As Long
    Dim numSheets As Long
    Dim wb As Workbook
    Dim summarySheet As Worksheet
    Dim sourceSheet As Worksheet

    Set wb = ThisWorkbook
    Set summarySheet = wb.Sheets("Summary Sheet")

    numSheets = ThisWorkbook.Sheets.Count  `My understanding is that this will return the total number of worksheets in the workbook. However, the sheet index seems to skip the number 5, so this may not be getting me the actual number of sheets
    sourceSheetIndex = 6                   `First sheet from which I want to pull values. Note that the sheets have inconsistent names, so I'm trying to use the sheet index.
    summarySheetTargetRow = 38             `Where I want to start plugging in copied cell values
    `Make sure receiving area for copied info is clear
    Sheets("Summary Sheet").Range("A38:D1415").ClearContents


    For sourceSheetIndex = 6 To numSheets
        Set sourceSheet = wb.Worksheets(sourceSheetIndex)

        DEBUG THORWS FAULT HERE[
        sourceSheetLastRow = sourceSheet.Range("M2:M1000").Find("*", SearchDirection:=xlPrevious).Row `I understand this to return the number of cells in the specified range, starting from the last non-empty cell.
        ]DEBUG THORWS FAULT HERE

        For sourceSheetTargetRow = 2 To sourceSheetLastRow `Start at second row because header rows will never have relevant value
            If sourceSheet.Range("I" & sourceSheetTargetRow) = "Text" Then
                summarySheet.Range("A" & summarySheetTargetRow) = sourceSheet.Range("A" & sourceSheetTargetRow)
                summarySheet.Range("B" & summarySheetTargetRow) = sourceSheet.Range("M" & sourceSheetTargetRow)
                summarySheet.Range("C" & summarySheetTargetRow) = sourceSheet.Range("H" & sourceSheetTargetRow)
                summarySheet.Range("D" & summarySheetTargetRow) = sourceSheet.Range("F" & sourceSheetTargetRow)
                summarySheetTargetRow = summarySheetTargetRow + 1
            End If
        Next sourceSheetTargetRow
    Next sourceSheetIndex

End Sub

标签: excelvba

解决方案


您何时复制单元格,列中的哪些条件需要为真/假?您可以使用 Union 一次选择多行单元格。


推荐阅读