我目前正尝试在多个工作表中扫描D & K(数量可能不同)。如果D列中的值是9或10,或者K列中的值大于100,我想将整行复制到汇总表中。它会创建摘要工作表,但不会复制任何行。这是我到目前为止:Excel VBA搜索多个工作表并将所选行粘贴到摘要工作表
Option Explicit
Sub AppendDataAfterLastColumn()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
Dim SearchRng As Range
Dim SearchRng1 As Range
Dim rngCell As Range
Dim lastrow As String
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Delete the summary worksheet if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Action Items").Delete
On Error GoTo 0
Application.DisplayAlerts = True
' Add a worksheet with the name "Action Items"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Action Items"
Sheets("Action Items").Move Before:=Sheets(3)
Sheets(4).Select
Range("A1:U3").Select
Selection.Copy
Sheets("Action Items").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveSheet.Paste
Range("a1") = "PFMEA Action Items"
' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
Set SearchRng = ActiveSheet.Range("D:D, K:K")
' Find the last row with data on the summary
' worksheet.
Last = Worksheets("Action Items").UsedRange.Rows.Count
For Each rngCell In SearchRng.Cells
If rngCell.Value <> "" Then
If rngCell.Value = "9" Or "10" Then
'select the entire row
rngCell.EntireRow.Select
MsgBox Selection.Address(False, False)
Selection.Copy
' This statement copies values, formats, and the column width.
lastrow = DestSh.Cells(Rows.Count, "A").End(xlUp).Row + 1
DestSh.Cells(lastrow, "A").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ElseIf rngCell.Value > 100 Then
'select the entire row
rngCell.EntireRow.Select
Selection.Copy
' This statement copies values, formats, and the column width.
lastrow = DestSh.Cells(Rows.Count, "A").End(xlUp).Row + 1
DestSh.Cells(lastrow, "A").PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
End If
Next rngCell
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
感谢您的帮助!
我注意到的一个问题是,在你的if语句中,如果rngCell.Value =“9”或者“10”那么你没有第二个标准。用'If rngCell.Value =“9或者rngCell.Value =”10“Then替换。另外,尽可能避免使用select语句是一种好的做法。只需在对象上直接运行方法。 :) – PartyHatPanda
感谢您的意见。我做了改变,但似乎没有改变任何东西。我认为我的问题与我选择,复制和粘贴的方式有关。 –