VBA从命名范围复制基于条件粘贴到另一个工作表中的Vlaues

时间:2017-03-22 17:22:17

标签: excel excel-vba conditional copy-paste vba

我有一个命名范围“数量”(工作表Sheet1,单元格I21:L28),它具有报告“L”列中数量的公式。我想在列L中搜索值> 0,然后将这些值(以及列K中的数据)粘贴到另一个工作表(Sheet10)中。 以下代码已关闭,但它粘贴公式而不是值。 请协助。

Sub CopyOnCondition()
     Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
     Set sh1 = Sheet1 'Edit sheet name
     Set sh2 = Sheet10 'Edit sheet name
     With sh1
         For Each c In .Range("L18:L24")
             If c.Value > 0 Then
                 c.Copy sh2.Cells(Rows.Count, 1).End(xlUp)(2, 1)
             End If
         Next
     End With
End Sub

2 个答案:

答案 0 :(得分:1)

试试这个

Sub CopyOnCondition()
     Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
     Set sh1 = Sheet1 'Edit sheet name
     Set sh2 = Sheet10 'Edit sheet name
     With sh1
         For Each c In .Range("L18:L24")
             If c.Value > 0 Then
                 sh2.Cells(Rows.Count, 1).End(xlUp)(2, 1).resize(,2).value=c.offset(,-1).resize(,2).value
             End If
         Next
     End With
End Sub

答案 1 :(得分:0)

当列L中的值>>时,下面的代码将仅复制列K:L中的值。 0

Sub CopyOnCondition()

     Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
     Set sh1 = Sheet1 'Edit sheet name
     Set sh2 = Sheet10 'Edit sheet name

     With sh1
         For Each c In .Range("L18:L24")
             If c.Value > 0 Then
                 c.Offset(, -1).Resize(1, 2).Copy '<-- copy column K with L
                 ' paste values to the first empty row in Column A of sh2
                 sh2.Cells(sh2.Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial xlValues
             End If
         Next c
     End With

End Sub