Home » excel » excel – VBA – Copy if last row is not equal to

excel – VBA – Copy if last row is not equal to

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m working on copying and pasting various sheets to a central “Import” tab within a workbook. The sheets are each a template, and some have been filled out while others have not. Regardless, because cells are filled by the template, there is never a truly “empty” sheet. I want to only bring in the data that has been added (beyond the template). If there has been no data added, the source last row = 11. How can I best modify my code to not copy the sheet if lrs=11 (but copy the data if lrs > 11), and move on to the next sheet instead?

For i = 1 To Sheets.Count
    If Sheets(i).Name <> "Import" And Sheets(i).Name <> "Cover page" And Sheets(i).Name <> "Introduction" And Sheets(i).Name <> "Additional Fuels" Then
        With Sheets(i)
            lrs = .Cells(.Rows.Count, "S").End(xlUp).Row 'Column S = "Specific Claim Language"
            .Range(.Cells(11, "B"), .Cells(lrs, "U")).Copy 'Data of interest exists from B to U
        End With
        With Sheets("Import")
            lrd = .Cells(.Rows.Count, "A").End(xlUp).Row
            .Range(.Cells(lrd + 1, "A"), .Cells(lrd + 1 + lrs, "AA")).PasteSpecial xlValues 'Only pasting data relevant to columns B:U on source, A:T on destination
        End With
    End If
Next i

How to&Answers:

Just check lrs is great than 11 before copying, and do the same before pasting.

Option Explicit
Sub test()
    For i = 1 To Sheets.count
        If Sheets(i).Name <> "Import" And Sheets(i).Name <> "Cover page" And Sheets(i).Name <> "Introduction" And Sheets(i).Name <> "Additional Fuels" Then
            With Sheets(i)
                lrs = .Cells(.Rows.count, "S").End(xlUp).Row 'Column S = "Specific Claim Language"
                If lrs > 11 Then
                    .Range(.Cells(11, "B"), .Cells(lrs, "U")).Copy 'Data of interest exists from B to U
                End if
            End With
            If lrs > 11 Then
                With Sheets("Import")
                    lrd = .Cells(.Rows.count, "A").End(xlUp).Row
                    .Range(.Cells(lrd + 1, "A"), .Cells(lrd + 1 + lrs, "AA")).PasteSpecial xlValues 'Only pasting data relevant to columns B:U on source, A:T on destination
                End With
            End If
        End If
    Next i 
End Sub

Alternatively just jump out after the <12 check and resume

Option Explicit
Sub test()
    For i = 1 To Sheets.count
        If Sheets(i).Name <> "Import" And Sheets(i).Name <> "Cover page" And Sheets(i).Name <> "Introduction" And Sheets(i).Name <> "Additional Fuels" Then
            With Sheets(i)
                lrs = .Cells(.Rows.count, "S").End(xlUp).Row 'Column S = "Specific Claim Language"
                If lrs < 12 Then GoTo nextsheet:
                .Range(.Cells(11, "B"), .Cells(lrs, "U")).Copy 'Data of interest exists from B to U
            End With
            With Sheets("Import")
                lrd = .Cells(.Rows.count, "A").End(xlUp).Row
                .Range(.Cells(lrd + 1, "A"), .Cells(lrd + 1 + lrs, "AA")).PasteSpecial xlValues 'Only pasting data relevant to columns B:U on source, A:T on destination
            End With
        End If
nextsheet:
    resume nextsheet2:
nextsheet2:
    Next i 
End Sub