无法使用VBA将整行从一张纸复制到另一张纸

佩佩

我正在尝试使用VBA做一个搜索工具,以在用户输入的给定SearchString的所有表中查找所有出现的事件。应该将在不同工作表中找到此SearchString的整个行粘贴到名为“搜索”的工作表的相应列中。如下图所示:

在此处输入图片说明

因此,应该在表格Free REQ中找到的行开始粘贴到单元格A11中,并粘贴到连续的行中,直到在表格Free REQ中找不到更多的对应关系为止,然后应该在表格Temporary中找到的行开始粘贴到单元格A11中。 N11,以此类推,适用于所有工作表(不同工作表中的表与“搜索”工作表中的相应表具有相同的列数)。为此,我有以下代码(TableColumn是“搜索”表中每个表开始的列,例如,对于免费REQ,它将为“ B”):

Private Sub OKButton_Click()
    Dim TableColumn As String
    Dim SearchString As String
    Dim SheetNames() As Variant
    Dim Loc As Range
    Dim CurrentRow

    SearchString = TextBox.Value

    'Sheets where we want to find stuff
    SheetNames = Array("Free REQs", "Temporary", "FTE", "Hierarchy", "all REQs", "EMEA TEAM")

    'Code to find all occurrences in all sheets MAKE SEPARATE SUB FOR THIS (from: http://stackoverflow.com/questions/19504858/find-all-matches-in-workbook-using-excel-vba)
    'Start filling the results table in row 11
    CurrentRow = 11

    For Each Sh In SheetNames
        TableColumn = GetTableBeginColumn(Sh)
        With Sheets(Sh)
            'Find in all sheets where SearchString ocurrs
            Set Loc = .UsedRange.Cells.Find(What:=SearchString)
            If Not Loc Is Nothing Then
                Do Until Loc Is Nothing
                    'Copy the data from the original source
                    .Rows(Loc.Row).EntireRow.Copy
                    'Paste it into the Search sheet
                    ActiveSheet.Paste Destination:=Sheets("Search").Range(TableColumn & CurrentRow)
                    'Find the next occurrence of SearchString
                    Set Loc = .UsedRange.FindNext(Loc)
                    'Fill the next empty row next
                    CurrentRow = CurrentRow + 1
                Loop
            End If
        End With
        CurrentRow = 11
        Set Loc = Nothing
    Next

End Sub

虽然,对于这一行:

ActiveSheet.Paste Destination:=Sheets("Search").Range(TableColumn & CurrentRow)

我收到错误消息:

在此处输入图片说明

即使TableColumn & CurrentRow对应于定义单个单元格的字符串。我在这里想念什么?

用户名

试试这个小重构

Option Explicit

Private Sub OKButton_Click()
    Dim TableColumn As String
    Dim SearchString As String
    Dim SheetNames() As Variant
    Dim Loc As Range
    Dim CurrentRow
    Dim sh As Variant
    Dim firstAddress As String

    SearchString = TextBox.Value

    'Sheets where we want to find stuff
    SheetNames = Array("Free REQs", "Temporary", "FTE", "Hierarchy", "all REQs", "EMEA TEAM")

    'Code to find all occurrences in all sheets MAKE SEPARATE SUB FOR THIS (from: http://stackoverflow.com/questions/19504858/find-all-matches-in-workbook-using-excel-vba)
    'Start filling the results table in row 11
    CurrentRow = 11

    For Each sh In SheetNames
        TableColumn = GetTableBeginColumn(sh)
        With Sheets(sh)
            'Find in all sheets where SearchString ocurrs
            Set Loc = .UsedRange.Cells.Find(What:=SearchString)
            If Not Loc Is Nothing Then
                firstAddress = Loc.Address '<--| store first found cell address
                Do
                    'Copy the data from the original source
                    Intersect(.Rows(Loc.Row).EntireRow, .UsedRange).Copy Destination:=Sheets("Search").Range(TableColumn & CurrentRow) '<--| copy only a "finite" amount of columns
                    'Find the next occurrence of SearchString
                    Set Loc = .UsedRange.FindNext(Loc)
                    'Fill the next empty row next
                    CurrentRow = CurrentRow + 1
                Loop While Loc.Address <> firstAddress '<--| loop until 'Find()' wraps back to first found cell
            End If
        End With
        CurrentRow = 11
        Set Loc = Nothing
    Next

End Sub

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将值从一张纸复制到另一张纸时出错

需要VBA宏-循环将数据从一张纸复制到另一张纸

仅将值从一张纸复制到另一张纸

从一张纸复制到另一张纸并复制到下一行

将范围从一张纸复制到另一张纸并粘贴多次,直到循环到下一张纸为止

将数据从一张纸复制到另一张纸的最有效的vba方法

如果满足三个不同条件,如何使用VBA将数据从一张纸复制到另一张纸?

使用VBA将数据从一张纸复制到另一张纸时出错

VBA将已过滤的字段从一张纸复制到另一张纸而不激活

使用VBA,将所有公式从一张纸复制到另一张纸上,而没有其他内容?

使用VBA将一行从一张纸复制到另一张纸

使用VBA将值从一张纸复制到另一张纸

将数据从一张纸复制到另一张纸的问题

VBA:从一张纸到另一张纸的多次复制和粘贴

将列从一张纸复制到另一张工作簿

将行从一张纸复制到另一张纸

将Excel行从一张纸复制到另一张纸

将文本从一张纸复制到另一张纸,具体取决于复制的来源

Excel根据条件将数据从一张纸复制到另一张纸

VBA - 将数据从一张纸复制到另一张纸

使用 vba 宏代码将特定列从一张纸复制到另一张纸

根据名称和日期将数据从一张纸复制到另一张纸

使用 vba 以相反的顺序将数据从一张纸复制到另一张纸

VBA,使用标题名称将值从一张纸复制到另一张纸

将列从一张纸复制到另一张纸的 VBA 代码出现间歇性“内存不足”错误

将行(不是整行)从一张纸复制到另一张纸

Excel宏将范围从一张纸复制到另一张纸

将粘贴值和格式从一张纸复制到另一张纸时出现 VBA 错误 1004

vba循环,以便将特定单元格从一张纸复制到另一张纸