PC Review


Reply
Thread Tools Rate Thread

Avoid paste error if nothing was copied.

 
 
J.W. Aldridge
Guest
Posts: n/a
 
      2nd Jul 2009
I run three consecutive codes named consolidate 1, 2, then 3.
Using the example below, I get an error if the copy range has no
data.
What is the best way to rewrite this or somehow avoid the error if
there is no data.
I want it to simply move on to the next step if there's nothing to
copy and or paste.

Sub Consolidate1()
Sheets("apples").Select
Range("a2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Summary").Select
Range("A65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Application.Goto Selection.Cells(1)
End Sub
 
Reply With Quote
 
 
 
 
Don Guillett
Guest
Posts: n/a
 
      2nd Jul 2009

Post ALL of your code. It can be greatly simplified

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"J.W. Aldridge" <(E-Mail Removed)> wrote in message
news:589d9fc2-cf83-492a-868e-(E-Mail Removed)...
>I run three consecutive codes named consolidate 1, 2, then 3.
> Using the example below, I get an error if the copy range has no
> data.
> What is the best way to rewrite this or somehow avoid the error if
> there is no data.
> I want it to simply move on to the next step if there's nothing to
> copy and or paste.
>
> Sub Consolidate1()
> Sheets("apples").Select
> Range("a2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Range(Selection, Selection.End(xlToRight)).Select
> Selection.Copy
> Sheets("Summary").Select
> Range("A65536").End(xlUp).Select
> ActiveCell.Offset(1, 0).Select
> Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
> False, Transpose:=False
> Application.Goto Selection.Cells(1)
> End Sub


 
Reply With Quote
 
J.W. Aldridge
Guest
Posts: n/a
 
      2nd Jul 2009
Sub Consolidate1()

Application.ScreenUpdating = False

Sheets("apples").Select

Range("a2").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select


Selection.Copy

Sheets("Summary").Select

Range("A65536").End(xlUp).Select


ActiveCell.Offset(1, 0).Select

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Application.Goto Selection.Cells(1)

End Sub

_______________________________
Sub Consolidate2()

Application.ScreenUpdating = False

Sheets("cherries").Select

Range("a2").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select


Selection.Copy

Sheets("Summary").Select

Range("A65536").End(xlUp).Select


ActiveCell.Offset(1, 0).Select

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Application.Goto Selection.Cells(1)

End Sub
____________________________
Sub Consolidate3()

Application.ScreenUpdating = False

Sheets("grapes").Select

Range("a2").Select

Range(Selection, Selection.End(xlDown)).Select

Range(Selection, Selection.End(xlToRight)).Select


Selection.Copy

Sheets("Summary").Select

Range("A65536").End(xlUp).Select

ActiveCell.Offset(1, 0).Select

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
Application.Goto Selection.Cells(1)

End Sub
 
Reply With Quote
 
EricG
Guest
Posts: n/a
 
      2nd Jul 2009
Use error trapping to skip the paste if it fails:

Sub Consolidate1()
Sheets("apples").Select
Range("a2").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Copy
Sheets("Summary").Select
Range("A65536").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
On Error Resume Next ' <<==== Skip the next line if it errors out
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
SkipBlanks:= _
False, Transpose:=False
On Error GoTo 0 ' <<==== Turn off error trapping
Application.Goto Selection.Cells(1)
End Sub

HTH,

Eric

"J.W. Aldridge" wrote:

> I run three consecutive codes named consolidate 1, 2, then 3.
> Using the example below, I get an error if the copy range has no
> data.
> What is the best way to rewrite this or somehow avoid the error if
> there is no data.
> I want it to simply move on to the next step if there's nothing to
> copy and or paste.
>
> Sub Consolidate1()
> Sheets("apples").Select
> Range("a2").Select
> Range(Selection, Selection.End(xlDown)).Select
> Range(Selection, Selection.End(xlToRight)).Select
> Selection.Copy
> Sheets("Summary").Select
> Range("A65536").End(xlUp).Select
> ActiveCell.Offset(1, 0).Select
> Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
> False, Transpose:=False
> Application.Goto Selection.Cells(1)
> End Sub
>

 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      2nd Jul 2009

This should be a bit more efficient. Run from anywhere in the workbook

Option Explicit
Sub copytosummary()
Dim dlr, slr, slc As Long
Dim ws As Worksheet

For Each ws In Worksheets
dlr = Sheets("summary"). _
Cells(Rows.Count, 1).End(xlUp).Row + 1
If ws.Name <> "Summary" Then
With ws
slr = .Cells(Rows.Count, "a").End(xlUp).Row
slc = .Cells(slr, "a").End(xlToRight).Column
.Cells(2, 1).Resize(slr, slc).Copy _
Sheets("Summary").Cells(dlr, 1)
End With
End If
Next ws
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"J.W. Aldridge" <(E-Mail Removed)> wrote in message
news:f6b7b1fa-a565-49e5-9261-(E-Mail Removed)...
> Sub Consolidate1()
>
> Application.ScreenUpdating = False
>
> Sheets("apples").Select
>
> Range("a2").Select
>
> Range(Selection, Selection.End(xlDown)).Select
>
> Range(Selection, Selection.End(xlToRight)).Select
>
>
> Selection.Copy
>
> Sheets("Summary").Select
>
> Range("A65536").End(xlUp).Select
>
>
> ActiveCell.Offset(1, 0).Select
>
> Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
> False, Transpose:=False
> Application.Goto Selection.Cells(1)
>
> End Sub
>
> _______________________________
> Sub Consolidate2()
>
> Application.ScreenUpdating = False
>
> Sheets("cherries").Select
>
> Range("a2").Select
>
> Range(Selection, Selection.End(xlDown)).Select
>
> Range(Selection, Selection.End(xlToRight)).Select
>
>
> Selection.Copy
>
> Sheets("Summary").Select
>
> Range("A65536").End(xlUp).Select
>
>
> ActiveCell.Offset(1, 0).Select
>
> Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
> False, Transpose:=False
> Application.Goto Selection.Cells(1)
>
> End Sub
> ____________________________
> Sub Consolidate3()
>
> Application.ScreenUpdating = False
>
> Sheets("grapes").Select
>
> Range("a2").Select
>
> Range(Selection, Selection.End(xlDown)).Select
>
> Range(Selection, Selection.End(xlToRight)).Select
>
>
> Selection.Copy
>
> Sheets("Summary").Select
>
> Range("A65536").End(xlUp).Select
>
> ActiveCell.Offset(1, 0).Select
>
> Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,
> SkipBlanks:= _
> False, Transpose:=False
> Application.Goto Selection.Cells(1)
>
> End Sub


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: how can I paste all the copied cells Gord Dibben Microsoft Excel Setup 0 3rd Jan 2007 06:02 PM
HOW TO AVOID MY PAGES BEING COPIED FRANK Microsoft Frontpage 21 28th Nov 2005 12:09 PM
Avoid blank lines when saving copied data from an excel sheet as a text file. Excel Beginner Microsoft Excel Programming 1 29th Nov 2004 05:30 PM
paste is not pasting what i copied =?Utf-8?B?bkF6?= Microsoft Word Document Management 1 28th Oct 2004 08:49 PM
Copied formulas paste as text and paste special does not work JJ Microsoft Excel Worksheet Functions 0 14th Sep 2003 08:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:38 PM.