Excel VBA to Copy paste using with statement to return message box

Ann Ooi

New Member
Joined
Jun 12, 2020
Messages
40
Office Version
  1. 365
Platform
  1. Windows
I need some advice to rectify the code below. I have this code to copy paste lines to another sheet for data compilation purpose. And I'm running well using the with statement below, the problem is, when there's no data to paste, I do not know how to end the code with message box. I see the similar question above, how to comply the code into the With statement of VBA below? Following is the code I read from other user, to return message box if error. {If Err Then MsgBox "Nothing to paste!" Err.Clear End If}.
Or should I change the coding? If so, what is the correct coding to apply?

My original code, without the Message box return.

VBA Code:
[/
Sub FnLstRow()
Application.ScreenUpdating = False
Dim LR As Long
ThisWorkbook.Worksheets("Data").Select
LR = Cells(Rows.Count, "AO").End(xlUp).Row
Cells(LR, 1).Offset(1, 0).EntireRow.Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
With Sheets("LatestData")
    .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues

End With
Application.CutCopyMode = False


Range("A1").Select
Application.ScreenUpdating = True
End Sub
]
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
When there is no data to paste ...
... is Cells(LR, 1).Offset(1, 0) empty ?

Rich (BB code):
Sub FnLstRow()
    Application.ScreenUpdating = False
    Dim LR As Long
    ThisWorkbook.Worksheets("Data").Select
    LR = Cells(Rows.Count, "AO").End(xlUp).Row
    Cells(LR, 1).Offset(1, 0).EntireRow.Select
    If Selection.Cells(1, 1) <> "" Then
        Cells(LR, 1).Offset(1, 0).EntireRow.Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        With Sheets("LatestData")
            .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        End With
        Application.CutCopyMode = False
        Range("A1").Select
    Else
        MsgBox "No Data", vbExclamation, ""
    End If
End Sub
 
Upvote 0
Hi Yongle,
Yes, you are correct, the row A is header, and the cells below, which is {Cells(LR, 1).Offset(1, 0)} are empty. But I applied the code you stated, it's still not working, it's return the same error, which is something "out of memory etc".
 
Upvote 0
VBA Code:
Sub FnLstRow()
    Application.ScreenUpdating = False
    Dim LR As Long, data As Worksheet, latest As Worksheet
    Set data = Sheets("Data")
    Set latest = Sheets("LatestData")
    LR = data.Cells(Rows.Count, "AO").End(xlUp).Row
    If LR > 1 Then
        data.Rows(2).Resize(LR).Copy
        latest.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        latest.Activate
        Range("A1").Select
    Else
        MsgBox "No Data", vbExclamation, ""
    End If
End Sub
 
Upvote 0
Hi Yongle,
Thank you so much, it's working fine now. You are superb!
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,847
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top