通过VBA将值从一行复制到另一个工作簿

时间:2014-09-04 22:33:07

标签: excel vba conditional-statements rows

如果条件为真,我有这个代码将行从一个工作表复制到另一个,但我有一点问题。 Sheet1的单元格有公式,我想只将值粘贴到Sheet2。 我怎么能在这段代码中做到这一点?

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

1 个答案:

答案 0 :(得分:1)

替换此行:

cell.EntireRow.Copy Sheets("Sheet2").Cells(i + 1, 1)

改为使用这两行:

cell.EntireRow.Copy
Sheets("Sheet2").Cells(i + 1, 1).PasteSpecial xlPasteValues