Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit Macro to copy data to second Excel file 2

Status
Not open for further replies.

ceil32

MIS
Apr 8, 2008
263
IE
I have a Macro that copies a selected range of data from part on an Excel file into a different Excel file and prints the copied data to the default printer.

I would like to modify the Macro to also copy the data to a second excel file - the same file and worksheet each time - and for this data to be overwritten each time the Macro is ran. The new file is called 'Upload.xls' and is located in the same folder as the other files

Can anyone assist? the VB Code is below

The Macro works on any version of Excel:

Thanks in advance

-----------



Sub only8elements()
'
' only12501 Macro
' Macro recorded 03/09/2007 by X
'

'
Range("A48:D59").Select
Selection.Copy
Windows("C4SHEET.xls").Activate
Sheets("12501").Select
Range("A25").Select
ActiveSheet.Paste
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("B26").Select
ActiveWindow.LargeScroll Down:=-1
Range("A1:J19").Select
Application.CutCopyMode = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
End Sub
 
Well, I'm not sure an Excel.Window object exposes a Sheets collection ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I've triple checked and all names match and this is the instance running the code.
 


I never use the Windows Collection, but I assumed, perhaps wrongly.

So instead of Windows(Workbook Name)

use Workbooks{/b](Workbook Name)

But this error, seemed to happen on a subsequent macro, where OTHERS WORKED!?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I changed all three Macro's from Windows.workbook name to Workbooks.woorbookname

e.g: With Workbooks("C4SHEET.xls").Sheets("Fe base")
.Range("A24").PasteSpecial Paste:=xlValues

All three macros have the same format.


When I run 'Fe base', I get the following error:

"Run-time error '1004'
PasteSpecial method of Range class failed."

The following code is highlighted yellow

".Range("A24").PasteSpecial Paste:=xlValues"


Exact same error for 'NiBSi_CrFe'

And when I run 'only1518', I get the following error:

"Run-time error '9'. Subscript out of range"

The following line of code is highlighted yellow:

"ThisWorkbook.Sheets("1518").Range("A46:D56").Copy"
 



You probably have nothing in your Clipboard.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


Did you TRY to RE COPY the range, before the paste?

Help yourself out!

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I'm editing an existing macro which is all automated - I do not physically copy anything.

 


Did you TRY to RE COPY the range, before the paste, in your macro?



Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've tried manually copying the range of cells and then running the Macro - I get the exact same error as above:

e.g: With Workbooks("C4SHEET.xls").Sheets("Fe base")
.Range("A24").PasteSpecial Paste:=xlValues

All three macros have the same format.


When I run 'Fe base', I get the following error:

"Run-time error '1004'
PasteSpecial method of Range class failed."

The following code is highlighted yellow

".Range("A24").PasteSpecial Paste:=xlValues"


Exact same error for 'NiBSi_CrFe'

And when I run 'only1518', I get the following error:

"Run-time error '9'. Subscript out of range"

The following line of code is highlighted yellow:

"ThisWorkbook.Sheets("1518").Range("A46:D56").Copy"
 
Seems like the workbook that contains the only1518 procedure don't have a sheet named 1518.

Anyway, avoid a numeric sheet name.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The workbook '1518' with no leading spaces is definitely there

The code refers to:

Sub only1518()
ThisWorkbook.Sheets("1518").Range("A46:D56").Copy
With Workbooks("Copy of C4SHEET.XLS").Sheets("only1518")
.Range("A24").PasteSpecial Paste:=xlValues
End With
With Windows("DEMupload.XLS").Sheets("DEM")
.Range("A24").PasteSpecial Paste:=xlValues
.PageSetup.PrintArea = Range("print_1518").Address
.PrintOut Copies:=1
End With
End Sub

---------

So the file 'Copy of C4.xls' has a worksheet called 'only1518' and the range I want to copy exists.

 
Code:
   With Workbooks("[b]Copy of C4[red]SHEET[/red].XLS[/b]").Sheets("only1518")
So the file 'Copy of C4.xls' has a worksheet called 'only1518' and the range I want to copy exists.
[red]THIS[/red] is why I have a difficult time believing that it is what you say it is.

"Run-time error '9'. Subscript out of range" comes from a MISSPELLED or missing reference IN YOUR CODE. No amount of "any suggestions" will make the solution any different.

CHECK! RECHECK and CHECK AGAIN...

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've checked over & over - I've renamed the files with no spaces, renamed the worksheets - the files & worksheets exist as in the code
 

You're gonna have to be your own sleuth...

faq707-4594

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top