![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Guest
Posts: n/a
|
I want to write a macro that will select the pages in an open workbook and then save them to a new workbook.
The following example works provided the sheets have the name 1,2 or 3. My problem is that the sheets do not always have the same name. How can I modify this macro? or is there a better way. Sub Macro1() ' Sheets(Array("1", "2", "3")).Select Sheets("3").Activate Sheets(Array("1", "2", "3")).Copy Before:=Sheets(1) End Sub Thanks John |
|
|
|
#2 | |
|
Guest
Posts: n/a
|
Quote:
Workbooks("Book1").Worksheets.Copy Before:=Workbooks("Book2").Sheets(1) |
|
|
|
|
#3 |
|
Guest
Posts: n/a
|
Thanks, but this produces a run time 9 error (subscript out of range)
|
|
|
|
#4 |
|
Guest
Posts: n/a
|
You have to change "Book1" and "Book2" to the names of your workbooks.
|
|
|
|
#5 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Try this...
Sub SaveSheets_NewBook() Dim Sh As Worksheet For Each Sh In ActiveWorkbook.Sheets Sh.Select False Next Sheets.Copy Windows(ThisWorkbook.Name).Activate End Sub HTH Ivan |
|
|
|
|
|
#6 |
|
Guest
Posts: n/a
|
Thanks. Yes I knew I needed to change book1 and book2. More checking shows the problem lies in the book1 end, removing this reference allows the macro to run, but without much control.
In response to Ivans message this produces a runtime error 1004, method select of object_worksheet failed. Prior to Ivans message I had modified the first response to: Workbooks.Add Template:= _ "C:Program FilesMicrosoft OfficeTest.xlt" Windows("test.xls").Activate Worksheets.Copy Before:=Workbooks("Test1").Sheets(1) and this seems to work. |
|
|
|
#7 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Winnipeg
Posts: 2,330
|
Quote:
ActiveWorkbook.Worksheets.Copy This will copy all the worksheets in to a new workbook. Regards,
__________________
Barrie Davidson "You're only given a little spark of madness. You mustn't lose it." - Robin Williams |
|
|
|
|
|
|
#8 |
|
Guest
Posts: n/a
|
I thought all was ok vut the problem is that it is copying the hidden worksheets as well. Can anyone help me set it up so it will not copy the hidden worksheets?
Thanks John |
|
|
|
#9 | |
|
Guest
Posts: n/a
|
Quote:
Hope this helps, Russell |
|
|
|
|
#10 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Portland, OR USA
Posts: 1,374
|
Sorry, that last post was me.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|