简体   繁体   English

如何在第1行中搜索部分文本并将整列复制/粘贴到新工作表上

[英]How to search for part of a text in row 1 and copy/paste that entire column onto a new sheet

I'm trying to search for partial text in row 1 of my spreadsheet. 我正在尝试在电子表格的第1行中搜索部分文本。 If a cell in row 1 contains that text, then I want to paste the entire column onto a new sheet in my workbook. 如果第1行中的单元格包含该文本,那么我想将整列粘贴到工作簿中的新工作表上。

I receive a report every morning with 50+ columns but I am only concerned with about 5 of those columns. 我每天早上收到一份包含50多个专栏的报告,但我只关心其中的5列。 The problem is that the columns are not always in the same order, so I can't just write code to copy Column C every day. 问题在于列的顺序并不总是相同,因此我不能只写代码每天复制列C。

Please see the below Sample Data. 请参见下面的示例数据。 For example, I would like to search row 1 for any cells that contain "Tomato" and copy the entire column to a new sheet. 例如,我想在第1行中搜索包含“西红柿”的所有单元格,然后将整列复制到新的工作表中。

Sample Data 样本数据

The only code I can find is below. 我可以找到的唯一代码如下。 However, it deals with copying a certain row if the text is found. 但是,如果找到了文本,它将处理复制特定的行。 Additionally, I'm not sure if the code is searching for a partial match or exact match. 另外,我不确定代码是否在搜索部分匹配或完全匹配。

Private Sub CommandButton1_Click()
   Dim LSearchRow As Integer
   Dim LCopyToRow As Integer

   On Error GoTo Err_Execute

   'Start search in row 4
   LSearchRow = 4

   'Start copying data to row 2 in Sheet2 (row counter variable)
   LCopyToRow = 2

   While Len(Range("A" & CStr(LSearchRow)).Value) > 0

      'If value in column E = "Mail Box", copy entire row to Sheet2
      If InStr("A:AZ" & CStr(1)).Value = "Country" Then

         'Select row in Sheet1 to copy
         Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
         Selection.Copy

         'Paste row into Sheet2 in next row
         Sheets("Sheet2").Select
         Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
         ActiveSheet.Paste

         'Move counter to next row
         LCopyToRow = LCopyToRow + 1

         'Go back to Sheet1 to continue searching
         Sheets("Sheet1").Select

      End If



   Wend

   'Position on cell A3
   Application.CutCopyMode = False
   Range("A3").Select

   MsgBox "All matching data has been copied."

   Exit Sub

Err_Execute:
   MsgBox "An error occurred."
End Sub

Any help on this is greatly appreciated! 在此方面的任何帮助将不胜感激!

Try this: 尝试这个:

Sub SearchFirstRowPasteEntireColumn()

    With Sheets("Sheet1")
        LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
        ColumnCounter = 1
        For col = 1 To LastColumn
            If .Cells(1, col) Like "*SEARCH KEY HERE*" Then
                .Columns(col).Copy Destination:=Sheets("Sheet2").Columns(ColumnCounter)
                ColumnCounter = ColumnCounter + 1
            End If
        Next
    End With

End Sub

The macro search for a cell in the first row of Sheet1 containing "SEARCH KEY HERE", and if it finds anything, it copies to the first blank column of the Sheet2. 宏在Sheet1的第一行中搜索包含“ SEARCH KEY HERE”的单元格,如果找到任何内容,它将复制到Sheet2的第一空白列。

The macro keeps searching till the last Sheet1's column, and pasting to the next blank column of Sheet2 宏会一直搜索到Sheet1的最后一列,然后粘贴到Sheet2的下一个空白列

This should work and copies across a range by finding the last row of the column you're copying. 通过找到要复制的列的最后一行,这应该可以正常工作并在一定范围内进行复制。

I've set up worksheet objects since I'm not sure what your sheets are called, and you can simply change the reference to them once at the start of the routine ( Sheet1 / Sheet2 ) and not worry about it in rest of code. 我已经建立了工作表对象,因为我不确定您的工作表是什么,您可以在例程开始时( Sheet1 / Sheet2 )更改一次对它们的引用,而不必担心其余的代码。 Also added a variable for your Keywords, you might wish to change this easily by implementing a function or looping through a list of keywords in a range or something. 另外,还为您的关键字添加了一个变量,您可能希望通过实现函数或循环浏览某个范围内或某个范围内的关键字列表来轻松更改此设置。

Sub eh()

Dim WS1 As Worksheet
Dim WS2 As Worksheet
Dim lCol1 As Long
Dim lCol2 As Long
Dim lRow As Long
Dim i As Long

Dim MY_TEXT_TO_MATCH As String

MY_TEXT_TO_MATCH = "tomato"

Set WS1 = ThisWorkbook.Worksheets("Sheet1")
Set WS2 = ThisWorkbook.Worksheets("Sheet2")

    lCol1 = WS1.Cells(1, WS1.Columns.Count).End(xlToLeft).Column

For i = 1 To lCol1
    If WS1.Cells(1, i).Value2 Like "*" & MY_TEXT_TO_MATCH & "*" Then
        lCol2 = WS2.Cells(1, WS2.Columns.Count).End(xlToLeft).Column + 1
        lRow = WS1.Cells(WS1.Rows.Count, i).End(xlUp).Row
        WS2.Range(Cells(1, lCol2).Address, Cells(lRow, lCol2).Address).Value = _
            WS1.Range(Cells(1, i).Address, Cells(lRow, i).Address).EntireColumn.Value
    End If
Next i

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索特定值并复制行以从整个工作簿粘贴到新工作表中 - Search Specific Value and Copy Row to paste in new Sheet from Entire Workbook Excel宏从一张工作表中搜索第1行中的某些文本,复制该文本下的列,然后粘贴到另一张工作表中 - Excel Macro to search certain texts in row 1 from one sheet, copy the column under the text, and paste to a different sheet Excel VBA-在特定列中搜索#N / A,将整行复制到新工作表中并删除行 - Excel VBA - Search certain column for a #N/A, copy entire row to new sheet and delete row 在列中搜索值并将行复制到新工作表 - Search for a value in a column and copy row to new sheet 如果其中的列包含指定值(新工作表中另一列的值),如何将行复制到另一个 Excel 工作表上? - How to copy a row onto another excel worksheet if a column in it contains a specified value (of another column in the new sheet)? 在特定行上搜索值,将整列复制到另一张纸上 - Search on specific row for value, copy entire column to another sheet 复制行,粘贴到其他工作表,然后将文本粘贴到列,然后转置 - Copy row, paste to different sheet, then text to column, then transpose 在另一个工作表的列中的工作表的列中搜索每个值,如果找到,则将整个行粘贴到输出中 - Search a each value from a column of sheet in another sheet's column and if find then paste entire row in output 将选定的单元格复制并粘贴到整行,直到上面的列为空 - Copy and Paste Selected Cell onto entire row until Column above is empty 找到一个短语,复制行,然后在上方找到非空单元格,然后将批次粘贴到新表上 - Locate a phrase, copy the row and then locate non-empy cell above and paste the lot onto a new sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM