根据其他条件粘贴到一个列中

时间:2019-02-04 03:47:06

标签: excel vba

我试图将一系列公式从一个工作表粘贴到另一个工作表。 在目标工作表中,代码在A列中查找条件,如果满足,则在H列中粘贴。它从最后使用的单元格开始。 我确信这是入门级的东西,但是如果有人可以提供帮助,将不胜感激。 下面的代码

Sub Step8()

'Copies cells from worksheet called "Bi-Weekly"

    Worksheets("Bi-Weekly").Activate
    Range("H16:BK16").Copy


'Go to target worksheet called "Report"

    Worksheets("Report").Activate

    Dim lRow As Long


'find last row

    lRow = Cells(Rows.Count, 1).End(xlUp).Row

'Loop from the last row to the first (finishing at row 17)

    For i = lRow To 17 Step -1

'Where column A = "No", paste copied cells to column H (to BK) from original worksheet

    If ActiveSheet.Range("A" & i).Value = "No" Then
    ActiveSheet.Range("H" & i).Paste
    End If
Next i


End Sub

1 个答案:

答案 0 :(得分:1)

复制公式

代码

Sub Step8()

    Const cSource As String = "Bi-Weekly" ' Source Worksheet Name
    Const cRange  As String = "H16:BK16"  ' Source Range Address
    Const cTarget As String = "Report"    ' Target Worksheet Name
    Const cColCrit As Variant = 1         ' Target Criteria Column Letter/Number
    Const cColTgt As Variant = "H"        ' Target Column Letter/Number
    Const cfRow As Long = 17              ' Target First Row
    Const cCrit As String = "No"          ' Target Criteria

    Dim rng As Range  ' Source Range
    Dim lRow As Long  ' Target Last Row Number
    Dim i As Long     ' Target Worksheet Row Counter

    ' Create a reference to the Source Range (rng).
    Set rng = ThisWorkbook.Worksheets(cSource).Range(cRange)
    ' In Target Worksheet
    With ThisWorkbook.Worksheets(cTarget)
        ' Calculate Last Row Number (lRow)
        ' from Target Criteria Column (cColCrit).
        lRow = .Cells(.Rows.Count, cColCrit).End(xlUp).Row
        ' Loop through rows (cells) of Target Worksheet starting from First Row.
        For i = cfRow To lRow
            ' When the cell at the intersection of the current row (i)
            ' and the Target Criteria Column (cColCrit) contains
            ' the Target Criteria (cCrit).
            If .Cells(i, cColCrit).Value = cCrit Then
                ' Copy Source Range (rng) to the cell at the intersection
                ' of the current row (i) and Target Column (cColTgt).
                rng.Copy .Cells(i, cColTgt)
            End If
        Next
    End With

End Sub

没有常量版本

Sub Step8NoConstants()

    Dim rng As Range  ' Source Range
    Dim lRow As Long  ' Target Last Row Number
    Dim i As Long     ' Target Worksheet Row Counter

    ' Create a reference to the Source Range (rng).
    Set rng = ThisWorkbook.Worksheets("Bi-Weekly").Range("H16:BK16")
    ' In Worksheet "Report".
    With ThisWorkbook.Worksheets("Report")
        ' Calculate Last Row Number (lRow) from column 1 ("A").
        lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
        ' Loop through rows (cells) of worksheet "Report" starting from row 17.
        For i = 17 To lRow
            ' When the cell at the intersection of the current row (i)
            ' and column 1 ("A") contains "No".
            If .Cells(i, 1).Value = "No" Then
                ' Copy Source Range (rng) to the cell at the intersection
                ' of the current row (i) and column "H".
                rng.Copy .Cells(i, "H")
            End If
        Next
    End With

End Sub