Excel 我需要一个宏来搜索特定列中的单元格,并将所有行复制到指定工作簿

Excel 我需要一个宏来搜索特定列中的单元格,并将所有行复制到指定工作簿,excel,vba,Excel,Vba,我正在使用一个有数千行的电子表格,在这些行中,我需要搜索B列中以“E”开头的单元格,并将所有行复制到不同的工作簿中。我没有运气去寻找这个特定的问题,或者如果我找到了什么,那也不是我需要它去做的。电子表格每周更新一次,我需要一个宏来进行搜索、快速复制和粘贴,而无需我进行选择、复制和粘贴。非常感谢您的帮助。如果您可以更改行的顺序,则不需要宏: 选择单元格B1,单击“排序AZ”按钮,选择以E开头的行,然后使用“复制和粘贴”。如果您更改了格式并按照stenci的建议对数据进行了一些操作,则可以避免使用V

我正在使用一个有数千行的电子表格,在这些行中,我需要搜索B列中以“E”开头的单元格,并将所有行复制到不同的工作簿中。我没有运气去寻找这个特定的问题,或者如果我找到了什么,那也不是我需要它去做的。电子表格每周更新一次,我需要一个宏来进行搜索、快速复制和粘贴,而无需我进行选择、复制和粘贴。非常感谢您的帮助。

如果您可以更改行的顺序,则不需要宏:

选择单元格B1,单击“排序AZ”按钮,选择以E开头的行,然后使用“复制和粘贴”。

如果您更改了格式并按照stenci的建议对数据进行了一些操作,则可以避免使用VBA。但是,如果您需要保持工作表的格式不变,并且无法按照建议操作数据(例如,由于办公场所的官僚作风),那么下面是一个子程序,它可以满足您的要求。我确保添加了大量的注释来描述每一行的功能。我希望有帮助,祝你好运

Sub copyRows()

Dim i As Integer, j As Integer, k As Integer
Dim bReport As Workbook, bReport2 As Workbook
Dim Report As Worksheet, Report2 As Worksheet

Set Report = Excel.ActiveSheet 'This assumes your sheet with data is the active sheet.
Set bReport = Report.Parent
Set bReport2 = Excel.Workbooks.Add 'This opens a new workbook
Set Report2 = bReport2.Worksheets(1) 'This assigns the variable for the worksheet where we will be pasting our data.

Report2.Cells(1, 1).Value = "Copied Rows" 'This gives a title to the worksheet (helps to avoid having to identify _
                                            the initial iteration of our for...next loop

For i = 1 To Report.UsedRange.Rows.Count 'Loops once for each row in the worksheet.

    If InStr(1, Left(Report.Cells(i, 2).Value, 1), "e", vbTextCompare) = 1 Then 'This searches the first letter of the value. I used the instr function to make it case-insensitive.
                                                                                    'You could also disable the binary comparison for the subroutine to accomplish this, but why bother?
        Report.Cells(i, 1).EntireRow.Copy 'This copies the row
        j = Report2.UsedRange.Rows.Count + 1 'This finds the row below the last used row in the new worksheet/workbook.
        Report2.Cells(j, 1).EntireRow.Insert (xlDown) 'This pastes the copied rows.
    End If
Next i

End Sub

注意:Siddharth Routh可能建议使用.end查找最后使用的单元格,而不是使用usedrange.rows.count,但我已经习惯了这两种方法中的后一种。一旦我对前一种方法稍微熟悉一点,我就可以开始使用它。

使用
AutoFilter-以