Home » excel » excel – Pasting to one column based on criteria in another

excel – Pasting to one column based on criteria in another

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am trying to paste a range of formulas from one worksheet to another.
In the target worksheet, the code looks for criteria in column A, then if met, pasts in column H. It goes from the last used cell up.
I am sure this is entry level stuff but if someone can assist that would be greatly appreciated.
Code below

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
How to&Answers:

Copy Formulas

The Code

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

No Constants Version

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