0

我正在使用一个包含数千行的电子表格,并且在这些行之外,我需要在 B 列中搜索以“E”开头的单元格并将所有行复制到不同的工作簿。我在搜索这个特定问题时没有运气,或者如果我发现了一些不完全是我需要它做的东西。电子表格每周更新一次,我需要一个宏来快速进行搜索、复制和粘贴,而无需我进行选择、复制和粘贴。任何帮助将不胜感激。

4

2 回答 2

1

如果您更改了格式并按照 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,但我习惯了两者中的后者。一旦我对它更熟悉了,我可能会开始使用前一种方法。

于 2013-01-14T01:45:49.093 回答
0

如果您可以更改不需要宏的行的顺序:
选择单元格 B1,单击排序 AZ 按钮,选择以 E 开头的行,然后使用复制和粘贴。

于 2013-01-11T18:04:13.833 回答