Copy and Paste Macro help


Posted by Pankaja Narayanan on May 26, 2001 6:50 AM

I have recorded a macro to do the following:

I have a file called "M.xls" which contains a lot of information (volumes for a period of five years for about 15 major tests from about 500 centers conducted by our company). All I want to do is to be able to copy a part of the file into a different file (for example, one year data for a couple of tests). So I opened a file called "Book1.xls" and the file "M.xls" and started recording a macro. All I did was to put a couple of descriptive statements in "Book1.Xls" and then went to "M.xls" and copied some information and pasted the information into "Book1.xls". After pasting the information I needed in "Book1.xls" I saved the file in a different name.
Now that I have is a recorded macro, I wanted to test
how the macro was working. I opened a new file called "Book1.xls" and ran the macro. I am gettting an error in one of the recorded statements in the macro as follows:

1. Windows ("M.xls").Activate
2. Range ("A1:O2").Select
3. Windows ("Book1.xls").Activate
4. ActiveSheet.Paste
(column "A7" has already been selected in "Book1.xls")

The error is pointed in the 3rd line above

Windows ("Book1.xls").Activate

and the error is

Error type no.9
Subscript out of range

Can someone tell me hoe to fix this or is there a better way to do what I am trying to do.

Thanks

Pankaja

Posted by S N on May 26, 2001 10:15 AM

I'm not "expert" enough to write the code for you, but it seems to me that when you open the new workbook, it has the next sequential name (ie- Book2, Book3, Book4, etc) and therefore you get the error since the macro is looking for Book1 when it doesn't exist.

I would try to have the macro open the new workbook for you (rather than you doing it manually) and then (if the Macro still uses the Book# name) use the "ActiveWorkbook" command to paste to the new workbook.

Posted by Sean on May 26, 2001 2:14 PM

===================

One other point to add to the above from S.N. As soon as you have created the new file do a save as operation and then always reference the new file name that you have created. This way as you switch between different books you can still get to the one you want.

Hope it Helps.
Sean.

Posted by Pankaja Narayanan on May 26, 2001 7:42 PM

Re: Copy and paste Macro - Thanks for suggestions and still trying to get an answer

Thanks for the two people who have given the following suggestions to my Excel question.
Actually when I open the new workbook, if it is Book2or Book3 etc., I rename it as Book1 and still the error message comes. I also tried closing Excel program and starting again. Book1 shows up but the same error message comes. I haven't tried the other suggestion of asking Excel to open a workbook for me and run the Macro.
I am sure there must be a way of writing the correct code for copying a part of the data from one file and writing it to another file and I am also sure that somebody would be able to help me.

Posted by Dave Hawley on May 26, 2001 9:53 PM

Re: Copy and paste Macro - Thanks for suggestions and still trying to get an answer

Actually when I open the new workbook, if it is Book2or Book3 etc., I rename it as Book1 and still the error message comes. I also tried closing Excel program and starting again. Book1 shows up but the same error message comes. I haven't tried the other suggestion of asking Excel to open a workbook for me and run the Macro. I am sure there must be a way of writing the correct code for copying a part of the data from one file and writing it to another file and I am also sure that somebody would be able to help me.


Hi Pankaja

You need to omit the ".xls". If you will only have the two workbooks open you can use this. You need to run it from the Workbook you want to copy from.

Sub CopyBetween()
'Written by OzGrid Business Applications
'www.ozgrid.com
Dim wBook1 As String
Dim wBook2 As String
Dim sReply1 As Integer, sReply2 As String

Application.ScreenUpdating = False
Application.EnableEvents = False

On Error Resume Next
wBook1 = ActiveWorkbook.Name
ActiveWindow.ActivateNext
wBook2 = ActiveWorkbook.Name

If wBook2 = wBook1 Then
sReply1 = MsgBox("No other workbooks are open! Do you want to open one?", vbYesNo)
If sReply1 = vbNo Then Exit Sub
sReply2 = Application.Dialogs(xlDialogFindFile).Show
If sReply2 = "False" Then Exit Sub
End If

On Error GoTo 0

Windows(wBook1).Activate
Sheets("Sheet1").Range("A1:A10").Copy

Windows(wBook2).Activate
Sheets("Sheet2").Range("A7").PasteSpecial
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWorkbook.Close


Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub


Dave


OzGrid Business Applications



Posted by Pankaja Narayanan on May 29, 2001 8:54 AM

Re: Copy and paste Macro - THANK YOU very much, Dave, for writing the code to "Copy/Paste"

Thank very much, SN, Sean and Dave for giving me suggestions, writing the codes for my macro "Copy/Paste". I appreciate your taking the time to help me.

Pankaja

for the two people who have given the following suggestions to my Excel question. : Actually when I open the new workbook, if it is Book2or Book3 etc., I rename it as Book1 and still the error message comes. I also tried closing Excel program and starting again. Book1 shows up but the same error message comes. I haven't tried the other suggestion of asking Excel to open a workbook for me and run the Macro. : I am sure there must be a way of writing the correct code for copying a part of the data from one file and writing it to another file and I am also sure that somebody would be able to help me. :

Hi Pankaja You need to omit the ".xls". If you will only have the two workbooks open you can use this. You need to run it from the Workbook you want to copy from.

Sub CopyBetween() 'Written by OzGrid Business Applications 'www.ozgrid.com Dim wBook1 As String Dim wBook2 As String Dim sReply1 As Integer, sReply2 As String Application.ScreenUpdating = False Application.EnableEvents = False On Error Resume Next wBook1 = ActiveWorkbook.Name ActiveWindow.ActivateNext wBook2 = ActiveWorkbook.Name If wBook2 = wBook1 Then sReply1 = MsgBox("No other workbooks are open! Do you want to open one?", vbYesNo) If sReply1 = vbNo Then Exit Sub sReply2 = Application.Dialogs(xlDialogFindFile).Show If sReply2 = "False" Then Exit Sub End If On Error GoTo 0 Windows(wBook1).Activate Sheets("Sheet1").Range("A1:A10").Copy Windows(wBook2).Activate Sheets("Sheet2").Range("A7").PasteSpecial Application.CutCopyMode = False ActiveWorkbook.Save ActiveWorkbook.Close Application.ScreenUpdating = True Application.EnableEvents = True End Sub Dave