仅将值从一张纸复制到另一张纸-

时间:2018-08-15 17:02:54

标签: excel vba excel-vba

我试图根据条件从一张纸复制到另一张纸。现在我有了代码,但是它没有复制值。源工作表仅包含公式,并且我尝试使用或不使用格式来复制值。

我的代码是:

Sub CopyTrue()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Hidden Cable Setup")
Set Target = ActiveWorkbook.Worksheets("Main")

j = 2     ' Start copying to row 1 in target sheet
For Each c In Source.Range("O1:O20000")   ' Do 1000 rows
    If c = "True" Then
       Source.Rows(c.Row).Copy Target.Rows(j)
       j = j + 1
    End If
Next c
End Sub

1 个答案:

答案 0 :(得分:0)

您可以直接分配值,而无需使用Copy

Sub CopyTrue()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

' Change worksheet designations as needed
Set Source = ActiveWorkbook.Worksheets("Hidden Cable Setup")
Set Target = ActiveWorkbook.Worksheets("Main")

j = 2     ' Start copying to row 1 in target sheet
For Each c In Source.Range("O1:O20000").Cells
    If c.Value = "True" Then
       Target.Rows(j).Value = c.EntireRow.Value
       'or just selected columns:
       Target.Cells(j, 1).Resize(1, 4).Value = _
             c.EntireRow.Cells(1).Resize(1, 4).Value
       j = j + 1
    End If
Next c
End Sub
相关问题