PC Review


Reply
Thread Tools Rate Thread

Copy and paste between sheets error

 
 
carlos_ray86@hotmail.com
Guest
Posts: n/a
 
      16th Jul 2007
I am having a little problem with this code. It won't copy and paste
from one worksheet to another. I have used the same code for copy and
paste before but never in a find loop so I'm guessing maybe I have to
do something different. I also tried

Workbooks("Book1.xls").Worksheets(2).Range(Cells(y, 1), Cells(y,
2)).PasteSpecial Paste:=xlPasteAll

Worksheets(2).Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
Paste:=xlPasteAll

Worksheets("SatLog").Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
Paste:=xlPasteAll

None worked

With Worksheets(1).Range("A1:A500")
Set c = .Find("Substrate # 1", LookIn:=xlValues)

If Not c Is Nothing Then
firstAddress = c.Address
y = 1
Do

Range(c.Offset(0, 0), c.Offset(3, 1)).Copy
Workbooks("Book1.xls").Worksheets("SatLog").Range(Cells(y,
1), Cells(y, 2)).PasteSpecial Paste:=xlPasteAll <--------Errors here
Set c = .FindNext(c)
y = y + 4

Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With

 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      16th Jul 2007
If SatLog in book1.xls isn't the activesheet, then this line will fail.

Workbooks("Book1.xls").Worksheets("SatLog").Range(Cells(y, 1), _
Cells(y, 2)).PasteSpecial Paste:=xlPasteAll

Those cells() reference refer to the activesheet (if the code is in a general
module) or the worksheet that owns the code (if the code is behind a worksheet).

I'd use something like this (untested):


Dim DestCell as range 'in your declaration section (at the top)

With Worksheets(1).Range("A1:A500")
Set c = .Find("Substrate # 1", LookIn:=xlValues)

If Not c Is Nothing Then
firstAddress = c.Address
y = 1
Do

with workbooks("book1.xls").worksheets("satlog")
set destcell = .range(.cells(y,1),.cells(y,2))
end with

Range(c.Offset(0, 0), c.Offset(3, 1)).Copy
destcell.PasteSpecial Paste:=xlPasteAll

Set c = .FindNext(c)
y = y + 4

Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With





(E-Mail Removed) wrote:
>
> I am having a little problem with this code. It won't copy and paste
> from one worksheet to another. I have used the same code for copy and
> paste before but never in a find loop so I'm guessing maybe I have to
> do something different. I also tried
>
> Workbooks("Book1.xls").Worksheets(2).Range(Cells(y, 1), Cells(y,
> 2)).PasteSpecial Paste:=xlPasteAll
>
> Worksheets(2).Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
> Paste:=xlPasteAll
>
> Worksheets("SatLog").Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
> Paste:=xlPasteAll
>
> None worked
>
> With Worksheets(1).Range("A1:A500")
> Set c = .Find("Substrate # 1", LookIn:=xlValues)
>
> If Not c Is Nothing Then
> firstAddress = c.Address
> y = 1
> Do
>
> Range(c.Offset(0, 0), c.Offset(3, 1)).Copy
> Workbooks("Book1.xls").Worksheets("SatLog").Range(Cells(y,
> 1), Cells(y, 2)).PasteSpecial Paste:=xlPasteAll <--------Errors here
> Set c = .FindNext(c)
> y = y + 4
>
> Loop While Not c Is Nothing And c.Address <> firstAddress
> End If
> End With


--

Dave Peterson
 
Reply With Quote
 
carlos_ray86@hotmail.com
Guest
Posts: n/a
 
      16th Jul 2007
On Jul 16, 7:55 am, Dave Peterson <peter...@verizonXSPAM.net> wrote:
> If SatLog in book1.xls isn't the activesheet, then this line will fail.
>
> Workbooks("Book1.xls").Worksheets("SatLog").Range(Cells(y, 1), _
> Cells(y, 2)).PasteSpecial Paste:=xlPasteAll
>
> Those cells() reference refer to the activesheet (if the code is in a general
> module) or the worksheet that owns the code (if the code is behind a worksheet).
>
> I'd use something like this (untested):
>
> Dim DestCell as range 'in your declaration section (at the top)
>
> With Worksheets(1).Range("A1:A500")
> Set c = .Find("Substrate # 1", LookIn:=xlValues)
>
> If Not c Is Nothing Then
> firstAddress = c.Address
> y = 1
> Do
>
> with workbooks("book1.xls").worksheets("satlog")
> set destcell = .range(.cells(y,1),.cells(y,2))
> end with
>
> Range(c.Offset(0, 0), c.Offset(3, 1)).Copy
> destcell.PasteSpecial Paste:=xlPasteAll
>
> Set c = .FindNext(c)
> y = y + 4
>
> Loop While Not c Is Nothing And c.Address <> firstAddress
> End If
> End With
>
>
>
>
>
> carlos_ra...@hotmail.com wrote:
>
> > I am having a little problem with this code. It won't copy and paste
> > from one worksheet to another. I have used the same code for copy and
> > paste before but never in a find loop so I'm guessing maybe I have to
> > do something different. I also tried

>
> > Workbooks("Book1.xls").Worksheets(2).Range(Cells(y, 1), Cells(y,
> > 2)).PasteSpecial Paste:=xlPasteAll

>
> > Worksheets(2).Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
> > Paste:=xlPasteAll

>
> > Worksheets("SatLog").Range(Cells(y, 1), Cells(y, 2)).PasteSpecial
> > Paste:=xlPasteAll

>
> > None worked

>
> > With Worksheets(1).Range("A1:A500")
> > Set c = .Find("Substrate # 1", LookIn:=xlValues)

>
> > If Not c Is Nothing Then
> > firstAddress = c.Address
> > y = 1
> > Do

>
> > Range(c.Offset(0, 0), c.Offset(3, 1)).Copy
> > Workbooks("Book1.xls").Worksheets("SatLog").Range(Cells(y,
> > 1), Cells(y, 2)).PasteSpecial Paste:=xlPasteAll <--------Errors here
> > Set c = .FindNext(c)
> > y = y + 4

>
> > Loop While Not c Is Nothing And c.Address <> firstAddress
> > End If
> > End With

>
> --
>
> Dave Peterson- Hide quoted text -
>
> - Show quoted text -


Dave Thank you it did work. SatLog was not the active sheet but the
change you made to the code made it work out. Thanks again.

 
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
Copy&paste of several sheets Lorenz Microsoft Excel Programming 3 31st May 2007 05:09 PM
Copy&paste of several sheets Lorenz Microsoft Excel Misc 1 29th May 2007 10:08 PM
Copy-Paste sheets - error in the dates Aninha Microsoft Excel Worksheet Functions 1 28th Feb 2006 04:04 PM
Copy and paste between sheets dmg Microsoft Excel Programming 5 1st Nov 2005 12:56 PM
Copy-Paste between sheets in dif WB's =?Utf-8?B?a29ucGVnbw==?= Microsoft Excel Programming 2 5th Nov 2004 12:34 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:10 AM.