PC Review


Reply
Thread Tools Rate Thread

copying wkshts from one workbook to another

 
 
andrea
Guest
Posts: n/a
 
      21st Nov 2006
I am having trouble getting my code for copying worksheets from one
workbook to another to work. Here is my code in its entirety:

Sub Save_Report()

Dim rngAllFiles(), rngSelectFiles(), m, fileSaveName, wkshtName As
Variant
Dim a, s, i As Integer
Dim newBook As Object

rngAllFiles = Array("1 Cover.xls", "2 Table of Contents.xls", "3 Top
Ten.xls", "4 FX Impact.xls", "5A MTD IS.xls", "5B QTD IS.xls", "5C YTD
IS.xls", "6 Sales-Internal.xls", "7 Sales-WS.xls", "8A MTD GM.xls", "8B
QTD GM.xls", "8C YTD GM.xls", "9 Op Exp by Location.xls", "10 RD Exp by
Month.xls", "11 AZ versus Pr. Year.xls", "12 AZ versus Budgdt.xls", "13
Payroll by BU.xls", "14 PR Tax by BU.xls", "15 Supplies by BU.xls", "16
Catalog by BU.xls", "17 R&M by BU.xls", "18 A&P by BU.xls", "19 T&E
BU.xls", "20 LP&C BU.xls", "21 R&H by BU.xls", "22 Headcount.xls", "23
Payroll by Location.xls", "24 Payroll versus Pr. Year.xls", "25 Payroll
versus Budget.xls", "26 BS.xls", "27 AR.xls", "28 Inventory.xls", "29
Cap Ex.xls")
a = 0
s = 0
i = Workbooks("Save_Final_Op_Summary.xls").Worksheets("Save Final
Report").Cells(40, 2)

If i = 1 Then
ReDim rngSelectFiles(i)
Else: ReDim rngSelectFiles(i - 1)
End If

For r = 7 To 39
If ActiveSheet.Cells(r, 2) = "True" Then
rngSelectFiles(s) = rngAllFiles(a)
s = s + 1
Else: End If
a = a + 1
Next r

Set newBook = Workbooks.Add
fileSaveName = Application.GetSaveAsFilename("newBook", "Microsoft
Excel Workbook (*.xls), *.xls")
newBook.SaveAs Filename:=fileSaveName
For Each m In rngSelectFiles
Workbooks.Open Filename:=m
Workbooks(m).Sheets(1).Copy
before:=Workbooks(fileSaveName).Sheets(1)
fileSaveName.Activate
ActiveSheet.UsedRange.PasteSpecial Paste:=xlPasteValues
Next m

End Sub

I am getting a subscript out of range error on this line
"Workbooks(m).Sheets(1).Copy
before:=Workbooks(fileSaveName).Sheets(1)".

Can anyone help me with this?

Thx,

 
Reply With Quote
 
 
 
 
wisccal@googlemail.com
Guest
Posts: n/a
 
      22nd Nov 2006
The problem is that the function getSaveAsFileName returns a full path
like "c:\myFile.xls". The Workbook Collection's index can only be used
with file names, though. In your case Workbooks("myFile.xls"). The
following should fix this problem

Workbooks(Mid(fileSaveName, InStrRev(fileSaveName, "\") + 1)).Sheets(1)

InStrRev looks up the last occurrence of the file separator "\", and
Mid returns a substring from that position + 1 to fileSaveName's end.

Regards,
Steve

Also, you are referring to
andrea schrieb:

> I am having trouble getting my code for copying worksheets from one
> workbook to another to work. Here is my code in its entirety:
>
> Sub Save_Report()
>
> Dim rngAllFiles(), rngSelectFiles(), m, fileSaveName, wkshtName As
> Variant
> Dim a, s, i As Integer
> Dim newBook As Object
>
> rngAllFiles = Array("1 Cover.xls", "2 Table of Contents.xls", "3 Top
> Ten.xls", "4 FX Impact.xls", "5A MTD IS.xls", "5B QTD IS.xls", "5C YTD
> IS.xls", "6 Sales-Internal.xls", "7 Sales-WS.xls", "8A MTD GM.xls", "8B
> QTD GM.xls", "8C YTD GM.xls", "9 Op Exp by Location.xls", "10 RD Exp by
> Month.xls", "11 AZ versus Pr. Year.xls", "12 AZ versus Budgdt.xls", "13
> Payroll by BU.xls", "14 PR Tax by BU.xls", "15 Supplies by BU.xls", "16
> Catalog by BU.xls", "17 R&M by BU.xls", "18 A&P by BU.xls", "19 T&E
> BU.xls", "20 LP&C BU.xls", "21 R&H by BU.xls", "22 Headcount.xls", "23
> Payroll by Location.xls", "24 Payroll versus Pr. Year.xls", "25 Payroll
> versus Budget.xls", "26 BS.xls", "27 AR.xls", "28 Inventory.xls", "29
> Cap Ex.xls")
> a = 0
> s = 0
> i = Workbooks("Save_Final_Op_Summary.xls").Worksheets("Save Final
> Report").Cells(40, 2)
>
> If i = 1 Then
> ReDim rngSelectFiles(i)
> Else: ReDim rngSelectFiles(i - 1)
> End If
>
> For r = 7 To 39
> If ActiveSheet.Cells(r, 2) = "True" Then
> rngSelectFiles(s) = rngAllFiles(a)
> s = s + 1
> Else: End If
> a = a + 1
> Next r
>
> Set newBook = Workbooks.Add
> fileSaveName = Application.GetSaveAsFilename("newBook", "Microsoft
> Excel Workbook (*.xls), *.xls")
> newBook.SaveAs Filename:=fileSaveName
> For Each m In rngSelectFiles
> Workbooks.Open Filename:=m
> Workbooks(m).Sheets(1).Copy
> before:=Workbooks(fileSaveName).Sheets(1)
> fileSaveName.Activate
> ActiveSheet.UsedRange.PasteSpecial Paste:=xlPasteValues
> Next m
>
> End Sub
>
> I am getting a subscript out of range error on this line
> "Workbooks(m).Sheets(1).Copy
> before:=Workbooks(fileSaveName).Sheets(1)".
>
> Can anyone help me with this?
>
> Thx,


 
Reply With Quote
 
andrea
Guest
Posts: n/a
 
      22nd Nov 2006
Thanks! That did it and the macro works great now.

(E-Mail Removed) wrote:
> The problem is that the function getSaveAsFileName returns a full path
> like "c:\myFile.xls". The Workbook Collection's index can only be used
> with file names, though. In your case Workbooks("myFile.xls"). The
> following should fix this problem
>
> Workbooks(Mid(fileSaveName, InStrRev(fileSaveName, "\") + 1)).Sheets(1)
>
> InStrRev looks up the last occurrence of the file separator "\", and
> Mid returns a substring from that position + 1 to fileSaveName's end.
>
> Regards,
> Steve
>
> Also, you are referring to
> andrea schrieb:
>
> > I am having trouble getting my code for copying worksheets from one
> > workbook to another to work. Here is my code in its entirety:
> >
> > Sub Save_Report()
> >
> > Dim rngAllFiles(), rngSelectFiles(), m, fileSaveName, wkshtName As
> > Variant
> > Dim a, s, i As Integer
> > Dim newBook As Object
> >
> > rngAllFiles = Array("1 Cover.xls", "2 Table of Contents.xls", "3 Top
> > Ten.xls", "4 FX Impact.xls", "5A MTD IS.xls", "5B QTD IS.xls", "5C YTD
> > IS.xls", "6 Sales-Internal.xls", "7 Sales-WS.xls", "8A MTD GM.xls", "8B
> > QTD GM.xls", "8C YTD GM.xls", "9 Op Exp by Location.xls", "10 RD Exp by
> > Month.xls", "11 AZ versus Pr. Year.xls", "12 AZ versus Budgdt.xls", "13
> > Payroll by BU.xls", "14 PR Tax by BU.xls", "15 Supplies by BU.xls", "16
> > Catalog by BU.xls", "17 R&M by BU.xls", "18 A&P by BU.xls", "19 T&E
> > BU.xls", "20 LP&C BU.xls", "21 R&H by BU.xls", "22 Headcount.xls", "23
> > Payroll by Location.xls", "24 Payroll versus Pr. Year.xls", "25 Payroll
> > versus Budget.xls", "26 BS.xls", "27 AR.xls", "28 Inventory.xls", "29
> > Cap Ex.xls")
> > a = 0
> > s = 0
> > i = Workbooks("Save_Final_Op_Summary.xls").Worksheets("Save Final
> > Report").Cells(40, 2)
> >
> > If i = 1 Then
> > ReDim rngSelectFiles(i)
> > Else: ReDim rngSelectFiles(i - 1)
> > End If
> >
> > For r = 7 To 39
> > If ActiveSheet.Cells(r, 2) = "True" Then
> > rngSelectFiles(s) = rngAllFiles(a)
> > s = s + 1
> > Else: End If
> > a = a + 1
> > Next r
> >
> > Set newBook = Workbooks.Add
> > fileSaveName = Application.GetSaveAsFilename("newBook", "Microsoft
> > Excel Workbook (*.xls), *.xls")
> > newBook.SaveAs Filename:=fileSaveName
> > For Each m In rngSelectFiles
> > Workbooks.Open Filename:=m
> > Workbooks(m).Sheets(1).Copy
> > before:=Workbooks(fileSaveName).Sheets(1)
> > fileSaveName.Activate
> > ActiveSheet.UsedRange.PasteSpecial Paste:=xlPasteValues
> > Next m
> >
> > End Sub
> >
> > I am getting a subscript out of range error on this line
> > "Workbooks(m).Sheets(1).Copy
> > before:=Workbooks(fileSaveName).Sheets(1)".
> >
> > Can anyone help me with this?
> >
> > Thx,


 
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
Copying range from selected workbook to open workbook John Microsoft Excel Programming 2 11th Aug 2007 03:49 PM
copying worksheets to a new workbook without formulae referencing original workbook pjdeeb@gmail.com Microsoft Excel Programming 2 16th Oct 2006 07:31 PM
Copying data from workbook/sheets to another workbook/sheet =?Utf-8?B?eXVrb25fcGhpbA==?= Microsoft Excel Programming 0 26th Jul 2006 07:33 PM
loop through a column on a workbook copying data on each row to another workbook, then copy data back to the original workbook burl_rfc Microsoft Excel Programming 1 1st Apr 2006 08:48 PM
Newbie question: Matching data/2 wkshts copying info over dperry11273 Microsoft Excel Worksheet Functions 2 26th Jul 2005 06:39 AM


Features
 

Advertising
 

Newsgroups
 


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