0

I there I have the following problem: I would like to paste the results in a new sheet if the outcome is not "NO MATCH", how can I paste this in the new sheet and after the last used row? I get an error on the Active.Paste

Here is my code:

Public Sub CopyRows()
    Sheets("Koppeling data").Select
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 4).End(xlUp).Row
    ' Loop through each row
    For x = 3 To 10
        ' Decide if to copy based on column D
        ThisValue = Cells(x, 4).Value
        If ThisValue = "NO MATCH" Then

        Else
            Rows(x).Copy
            Sheets("All sessions").Select
            Call FindingLastRow
            ActiveSheet.Paste
            Sheets("Koppeling data").Select
        End If
    Next x
End Sub


Sub FindingLastRow()
    Dim sht As Worksheet
    Dim LastRow As Long

    Set sht = ThisWorkbook.Worksheets("All sessions")

    LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
End Sub
Daniel
  • 9,491
  • 12
  • 50
  • 66
Sner88
  • 1
  • 1
  • 6
  • 1
    What is the error you get? – K.Nicholas Mar 02 '16 at 15:32
  • It doesn't work correctly, because it doesn't paste after the last row, but it just paste the 10th row from "Koppeling data" sheet 10 times at the "All sessions" sheet – Sner88 Mar 02 '16 at 15:36
  • I provided an answer below, but so you understand why this code does not work the `ActiveSheet.Paste` is directly set after the worksheet is selected. The code never specifies **exactly** where to paste. You do call `FindingLastRow` but this just sets the last row of data to the variable `LastRow`. It does not select any cell, nor assign a range to work with. – Scott Holtzman Mar 02 '16 at 15:42

3 Answers3

2

Give this a shot. I simplified the code a lot, removed the .Select statements - which should be avoided at all costs- , and assigned variables to objects and worked directly with them.

Public Sub CopyRows()

Dim wsK As Worksheet, wsA As Worksheet
Set wsK = Sheets("Koppeling data")
Set wsA = Sheets("All sessions")

Dim FinalRow as Long
FinalRow = wsk.Cells(wsk.Rows.Count, 4).End(xlUp).Row

' Loop through each row in Koppeling data
For x = 3 To FinalRow

    ' Decide if to copy based on column D
    If wsK.Cells(x, 4).Value <> "NO MATCH" Then
        wsK.Rows(x).EntireRow.Copy _
            Destination:=wsA.Range("A" & wsA.Rows.Count).End(xlUp).Offset(1) 'used `.Offset(1)` here so it will paste one row below last row with data.
        'use this to paste values
        'wsk.Rows(x).Copy
        'wsA.Range("A" & wsA.Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End If

Next x

End Sub
Scott Holtzman
  • 27,099
  • 5
  • 37
  • 72
0

You have FindingLastRow as sub, which doesn't do anything. If you want to return the result (in your case the last row), you have to define it as a function:

Function FindingLastRow() as Long
     'Your existing code
     FindingLastRow = LastRow
End Function

This will return the value of last row, and in the main sub you can just paste into the following row:

Dim lastRow as Long
lastRow = FindingLastRow
ActiveSheet.Range("A" & lastRow + 1).Paste
ramesses
  • 74
  • 1
  • 3
  • 16
0

Try to get away from using Worksheet.Select and Range .Select to accomplish your actions.

Public Sub CopyRows()
    Dim x As Long, lastRow As Long, finalRow As Long

    With Worksheets("Koppeling data")
    ' Find the last row of data
        finalRow = .Cells(.Rows.Count, 4).End(xlUp).Row
        ' Loop through each row
        For x = 3 To 10 'For x = 3 To finalRow
            ' Decide if to copy based on column D
            If UCase(.Cells(x, 4).Value) <> "NO MATCH" Then
                FindingLastRow Worksheets("All sessions"), lastRow
                .Rows(x).Copy Destination:=Worksheets("All sessions").Range("A" & lastRow + 1)
            End If
        Next x
    End With
End Sub

Sub FindingLastRow(ws As Worksheet, ByRef lr As Long)
    With ws
        lr = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With
End Sub

The ByRef allows you to pass a previously declared variable into your helper sub and have that variable return with a changed value.


See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Community
  • 1
  • 1