[英]Copy and Paste various cells to another sheet when condition met
I need a little help because I´m trying to Copy and Paste selected cells from the same row to another sheet when condition are met, but I'm kind of lost, I hope you can help me我需要一点帮助,因为我正在尝试在满足条件时将选定的单元格从同一行复制并粘贴到另一张纸上,但我有点迷茫,希望你能帮助我
for example, I have the datasheet called sheet7 that contain data range(A4 to AF40) and my condition is that can copy and paste all rows that met Column E = "Contract" but to be able to copy some other cells, for example, columns D, G, O, and V例如,我有一个名为 sheet7 的数据表,其中包含数据范围(A4 到 AF40),我的条件是可以复制和粘贴所有符合 Column E =“Contract”但能够复制其他一些单元格的行,例如, D、G、O 和 V 列
I want to pass all that data to a new sheet called sheet10我想将所有这些数据传递给一个名为 sheet10 的新工作表
I hope it has been clear, and I hope you can help me我希望它已经清楚了,我希望你能帮助我
Private Sub CommandButton2_Click()
Dim range1 As Range
Dim Cell As Range
Set range1 = Sheet1.Range("E4:E100")
For Each Cell In range1
If Cell.Value = "Contract" Then
With Sheet7
.Range(.Cells(Cell.Row, "C"), .Cells(Cell.Row, "F")).Copy _
Sheet10.Range("B100").End(xlUp).Offset(1, 0)
End With
End If
Next
End Sub
Option Explicit
Private Sub CommandButton2_Click()
' Lookup Range
Dim llRow As Long
Set llRow = Sheet1.Range("E" & Sheet1.Rows.Count).End(xlUp).Row
Dim lrg As Range: Set lrg = Sheet1.Range("E4:E" & llRow)
' Source Columns Range
Dim scrg As Range
With Sheet7
Set scrg = Union(.Columns("D"), .Columns("G"), _
.Columns("O"), Columns("V"))
End With
' Destination Cell
Dim dfCell As Range
Set dfCell = Sheet10.Range("B" & Sheet10.Rows.Count).End(xlUp).Offset(1, 0)
Dim srg As Range ' Source Range
Dim lCell As Range ' Lookup Cell
For Each lCell In lrg.Cells
If StrComp(CStr(lCell.Value), "Contract", vbTextCompare) = 0 Then
' Combine cell into a range.
If srg Is Nothing Then
Set srg = Sheet7.Cells(lCell.Row, "A")
Else
Set srg = Union(srg, Sheet7.Cells(lCell.Row, "A"))
End If
End If
Next lCell
If srg Is Nothing Then Exit Sub
Set srg = Intersect(srg.EntireRow, scrg)
srg.Copy dfCell
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.