何千行もあるスプレッドシートを使用していますが、それらの行から、「 E」で始まる列 B のセルを検索し、すべての行を別のワークブックにコピーする必要があります。この特定の質問を検索するのに運がなかった、または何かを見つけた場合、それはまさに私が必要としていたものではありません. スプレッドシートは毎週更新され、選択、コピー、貼り付けを行わなくても、この検索とコピーと貼り付けをすばやく実行できるマクロが必要です。どんな助けでも大歓迎です。
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は、usedrange.rows.countを使用するのではなく、.endを使用して最後に使用されたセルを見つけることを提案する場合がありますが、私は2つの後者に慣れています。前者の方法に少し慣れたら、前者の方法を使い始めるかもしれません。
于 2013-01-14T01:45:49.093 に答える
0
マクロを必要としない行の順序を変更できる場合:
セル B1 を選択し、[AZ の並べ替え] ボタンをクリックして、E で始まる行を選択し、コピーと貼り付けを使用します。
于 2013-01-11T18:04:13.833 に答える