无法使用VBA将整个行从一个工作表复制到另一个工作表

我正在尝试使用VBA创buildsearch工具,以查找用户input的所有SearchStringinput表中的所有出现次数。 它应该粘贴在这个SearchString被find的整个行在不同的工作表在他们对应的列名为“search”的列中。 如下图所示:

在这里输入图像说明

因此,表单Free REQ中的行应该开始粘贴到单元格A11中,并粘贴到连续的行中,直到在表单中找不到更多的对应关系,然后在临时表单中find的行应该开始粘贴到单元格中N11等等(所有表格中的表格与search表格中的相应表格具有相同的列数)。 为此,我有以下代码(TableColumn是search表中的每个表开始的列,例如对于自由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对应于定义单个单元格的string。 我在这里错过了什么?

尝试这个小重构

 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