使用VBA中的复制目标代码仅复制值而不复制公式

时间:2020-08-12 19:52:37

标签: excel vba

我想复制过滤表中剩余的单元格,但只复制其值而不是公式。我使用了下面的cose,除非过滤器只留下一行,否则在[Sheets(“ Cost Accounting”)。Range(A2).PasteSpecial xlPasteValues]代码行中,我只会得到错误“脚本超出范围”的行,否则工作正常。 / p>

我附上了一个示例的图片,其中的代码将不会粘贴(当过滤器后仅剩一行时)Example Problem Scenario

有什么想法吗?

Sub DT_test()

Dim table As ListObject
Dim rngToCopy As Range

Sheets.Add.Name = ("Cost Accounting")
Sheets("Cost Accounting").Activate

TeamRole2 = "Cost Accounting"
MaxDate = Date

Set table = Worksheets("Overview").ListObjects("Table1")

table.Range.AutoFilter Field:=13, Criteria1:=TeamRole2
table.Range.AutoFilter Field:=8, Criteria1:="<" & MaxDate

On Error Resume Next
Set rngToCopy = table.DataBodyRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0

If Not rngToCopy Is Nothing Then
    rngToCopy.Copy
    Sheets("Cost Accounting").Range(A2).PasteSpecial xlPasteValues
End If

table.Range.AutoFilter

结束子

1 个答案:

答案 0 :(得分:0)

尝试粘贴值...

If Not rngToCopy Is Nothing Then
    rngToCopy.Copy 
    Sheets("Cost Accounting").Range("A2").PasteSpecial xlPasteValues
End If
相关问题