我有一个宏可以根据过滤器复制数据。但是一些正在复制的单元格中已经有公式,所以我想复制它并仅作为文本或值粘贴。我尝试使用下面的。我尝试了 xlValues 和 Format text,它们都给出了错误 - 运行时错误 '438' 。对象不支持此属性或方法。
Sub DS()
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
Dim sourceWorkbookPath As String
Dim targetWorkbookPath As String
Dim lastRow As Long
' Define workbooks paths
sourceWorkbookPath = "Exceptional Transfer -2020 v2.xlsm"
targetWorkbookPath = "template2.xlsx"
' Set a reference to the target Workbook and sheets
Set sourceWorkbook = Workbooks.Open(sourceWorkbookPath)
Set targetWorkbook = Workbooks.Open(targetWorkbookPath)
' definr worksheet's names for each workbook
Set sourceSheet = sourceWorkbook.Worksheets("A")
Set targetSheet = targetWorkbook.Worksheets("B")
With sourceSheet
' Get last row
lastRow = .Range("K" & .Rows.Count).End(xlUp).Row
.Range("A1:Q1").AutoFilter Field:=14, Criteria1:="PENDING"
.Range("A1:Q1").AutoFilter Field:=11, Criteria1:="U3R", Operator:=xlOr, Criteria2:="U2R"
.Range("K2:K" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("A1")
.Range("C2:C" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("B1")
.Range("E2:E" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("E1").PasteSpecial xlValues
.Range("G2:G" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("F1")
.Range("S2:S" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("I1")
.Range("T2:T" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("J1")
.Range("U2:U" & lastRow).SpecialCells(xlCellTypeVisible).Copy _
Destination:=targetSheet.Range("C1")
End With
On Error Resume Next
sourceSheet.ShowAllData
On Error GoTo 0
End Sub
Run Code Online (Sandbox Code Playgroud)
删除单行复制/粘贴Destination并切换到多行复制/粘贴方法,它使您可以访问该.PasteSpecial属性
'Copy here
.Range("K2:K" & lastrow).SpecialCells(xlCellTypeVisible).Copy
'Paste here
targetSheet.Range("A1").PasteSpecial xlPasteValues
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |