PDA

View Full Version : help with a message box on error



perksukataus
07-23-2008, 03:57 AM
Hello.

I'm trying to get a message box to come up when my code errors out. I'd like it to say "No data to copy" or some such.

Here's the code - the * marks the error spot (i think)

Sheets("Current Month Paste").Select
range("A2").Select
range(Selection, Selection.End(xlToRight)).Select
range(Selection, Selection.End(xlToRight)).Select
range(Selection, Selection.End(xlToRight)).Select
range(Selection, Selection.End(xlToRight)).Select
Rows("2:2").Select
range(Selection, Selection.End(xlDown)).Select
Selection.Copy



Sheets("Transactions Since Inception").Select

Dim LastCell As range
With ActiveSheet
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
If IsEmpty(LastCell) Then
'do nothing
Else
LastCell.Select
ActiveCell.Offset(1, 0).Select
* ActiveCell.PasteSpecial
range(Selection, Selection.End(xlDown)).Select

ilyaskazi
07-23-2008, 04:50 AM
Change this code..
* ActiveCell.PasteSpecial
range(Selection, Selection.End(xlDown)).Select

to this code...

On Error Resume Next
ActiveCell.PasteSpecial
range(Selection, Selection.End(xlDown)).Select
If Err.Number <> 0 then MsgBox "No data to copy"


HTH,
Ilyas Kazi

mdmackillop
07-23-2008, 09:01 AM
Option Explicit
Sub Test()
Dim Rng As Range
Dim LastCell As Range
With Sheets("Current Month Paste")
Set Rng = Range(.Rows("2:2"), .Rows("2:2").End(xlDown))
End With
If Application.CountA(Rng) = 0 Then
MsgBox "Nothing to copy"
Exit Sub
End If
With Sheets("Transactions Since Inception")
Set LastCell = .Cells(.Rows.Count, "A").End(xlUp)
If IsEmpty(LastCell) Then
'do nothing
Else
Rng.Copy LastCell
End If
End With
End Sub