通过VBA将行从一行复制到另一个工作簿
问题描述:
如果条件为真,我有此代码将表格中的行复制到另一个工作簿,但是我遇到了一些问题。 Sheet1的单元格具有公式,我只想将这些值粘贴到Sheet2。 如何在此代码中执行此操作?通过VBA将行从一行复制到另一个工作簿
Sub CopyRows()
Dim cell As Range
Dim lastRow As Long, i As Long
Dim FName As String
Dim FPath As String
Dim NewBook As Workbook
lastRow = Range("A" & Rows.Count).End(xlUp).Row
i = 1
For Each cell In Sheets("Sheet1").Range("T1:T" & lastRow)
If cell.Value = "X" Or cell.Value = "Y" Then
cell.EntireRow.Copy Sheets("Sheet2").Cells(i + 1, 1)
i = i + 1
End If
Next
End Sub
答
替换此行:
cell.EntireRow.Copy Sheets("Sheet2").Cells(i + 1, 1)
有了这两条线来代替:
cell.EntireRow.Copy
Sheets("Sheet2").Cells(i + 1, 1).PasteSpecial xlPasteValues