如何仅使用VBA复制值

时间:2015-02-23 06:05:38

标签: excel vba excel-vba

我只需将值从表格复制到另一个而不需要复制。以下代码仅复制但仅使用公式。我尝试了本网站提供的一些解决方案,但它们给了我错误。

For i = 2 To LastRow
    'sheet to copy from
   With Worksheets("Hoist")
        'check column H value before copying
       If .Cells(i, 8).Value >= -90 _
       And CStr(.Cells(i, 9).Value) <> "Approved" _
       And CStr(.Cells(i, 9).Value) <> "" _
       And CStr(.Cells(i, 10).Value) = "" Then
            'copy row to "display" sheet
           .Rows(i).Copy Destination:=Worksheets("display").Range("A" & j)
           j = j + 1
       End If
   End With
Next i

2 个答案:

答案 0 :(得分:2)

尝试更改此行:

.Rows(i).Copy Destination:=Worksheets("display").Range("A" & j)

到此:

.Rows(i).Copy
Worksheets("display").Range("A" & j).PasteSpecial xlPasteValues

然而,这会丢弃所有格式。要包含格式,您需要添加另一行,如:

Worksheets("display").Range("A" & j).PasteSpecial xlPasteFormats

答案 1 :(得分:0)

另一个选择是输入工作列并使用AutoFilter来避免循环

  • 在A列中插入一列
  • 工作栏formuka是=AND(I2>-90,AND(J2<>"",J2<>"Approved"),K2="")
  • 过滤并复制 TRUE
  • 删除工作栏A

Sub Recut()
Dim ws As Worksheet
Dim rng1 As Range

Set ws = Sheets("Hoist")
ws.AutoFilterMode = False
Set rng1 = Range([h2], Cells(Rows.Count, "H").End(xlUp))

ws.Columns(1).Columns.Insert
rng1.Offset(0, -8).FormulaR1C1 = "=AND(RC[8]>-90,AND(RC[9]<>"""",RC[9]<>""Approved""),RC[10]="""")"

With rng1.Offset(-1, -8).Resize(rng1.Rows.Count + 1, 1).EntireRow
    .AutoFilter Field:=1, Criteria1:="TRUE"
    .Copy Sheets("display").Range("A1")
    Sheets("display").Columns("A").Delete
End With

ws.Columns(1).Delete
ws.AutoFilterMode = False

End Sub