[英]VBA to Copy from Named Range based on Condition to Paste Vlaues in another worksheet
我有一个命名范围“数量”(工作表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
尝试这个
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
当L列中的值> 0时,以下代码将仅从K:L列中复制值。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.