有没有办法可以将以下代码更改为仅复制特定的单元格范围或列:
例如:我在从 A 到 Z 的所有列中都有数据。我想将数据复制到另一张工作表,但我只想从 A、D、H 和 J 列(A2、D2、H2、J2)复制数据。
Option Explicit
Private Sub Worksheet_Activate()
Dim LR As Long
Me.UsedRange.Offset(1).ClearContents 'clear existing data
With Sheets("Raw - Incident Request Report")
.AutoFilterMode = False 'remove any prior filtering
.Rows(1).AutoFilter 'activate autofilter
.Rows(1).AutoFilter 27, Criteria1:="Breached" 'filter column D for 80%+
LR = .Range("D" & .Rows.Count).End(xlUp).Row 'is any data visible?
If LR > 1 Then
.Range("AC7:AC" & LR).Copy Range("C3") 'copy any data visible to report
.Range("D7:D" & LR).Copy Range("D3")
.Range("I7:I" & LR).Copy Range("E3")
.Range("K7:K" & LR).Copy Range("F3")
.Range("T7:T" & LR).Copy Range("G3")
Else
Range("C3") = "No Data Found" 'if none, give that message
End If
.AutoFilterMode = False 'turn off autofilter
End With
End Sub
最终代码 - 编辑:
Option Explicit
Private Sub Worksheet_Activate()
Dim LR As Long
Me.UsedRange.Offset(17).ClearContents
With Sheets("Raw - Incident Request Report")
.AutoFilterMode = False
LR = .Range("D" & .Rows.Count).End(xlUp).Row
.Range("D6:AH" & LR).AutoFilter Field:=26, Criteria1:="<>"
If LR > 1 Then
.Range("AC7:AC" & LR).Copy
Sheets("Tickets").Range("C17").PasteSpecial xlPasteValues
.Range("D7:D" & LR).Copy
Sheets("Tickets").Range("D17").PasteSpecial xlPasteValues
.Range("I7:I" & LR).Copy
Sheets("Tickets").Range("E17").PasteSpecial xlPasteValues
.Range("K7:K" & LR).Copy
Sheets("Tickets").Range("F17").PasteSpecial xlPasteValues
.Range("T7:T" & LR).Copy
Sheets("Tickets").Range("G17").PasteSpecial xlPasteValues
Else
Range("C17") = "No Data Found"
End If
.AutoFilterMode = False
End With
End Sub