PC Review


Reply
Thread Tools Rate Thread

Combining several workbooks into one workbook

 
 
=?Utf-8?B?QmV0dHk=?=
Guest
Posts: n/a
 
      28th Mar 2007
While trying to run this macro, I get this error. Any insights?

Thanks
---------------------------
Microsoft Excel
---------------------------
Method 'Move' of object 'Sheets' failed
---------------------------
OK
---------------------------

Here is the Syntax

Sub CombineWorkbooks()
Dim FilesToOpen
Dim x As Integer

On Error GoTo ErrHandler
Application.ScreenUpdating = False

FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
MultiSelect:=True, Title:="Files to Merge")

If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If

x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open FileName:=FilesToOpen(x)
Sheets().Move After:=ThisWorkbook.Sheets _
(ThisWorkbook.Sheets.Count)
x = x + 1
Wend

ExitHandler:
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      28th Mar 2007
Comment out your error handler so you can see what the situation is. When
you get the error message, choose debug.

then see where it stops and what the value of your variables are.

It worked for me with vanilla workbooks. I saw problems with workbooks
that had links and workbooks that were password protected.

Anyway, the basic code appears to be sound and no you have to deal with
peculiarities/special situations in your workbooks.

(I assume you replaced the Wend with a Next. )

--
Regards,
Tom Ogilvy


"Betty" wrote:

> This is the error that I'm getting now
>
> ---------------------------
> Microsoft Excel
> ---------------------------
> Copy method of Worksheet class failed
> ---------------------------
> OK
> ---------------------------
>
>
> "Tom Ogilvy" wrote:
>
> > Sub CombineWorkbooks()
> > Dim FilesToOpen
> > Dim x As Integer
> >
> > On Error GoTo ErrHandler
> > Application.ScreenUpdating = False
> >
> > FilesToOpen = Application.GetOpenFilename _
> > (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
> > MultiSelect:=True, Title:="Files to Merge")
> >
> > If TypeName(FilesToOpen) = "Boolean" Then
> > MsgBox "No Files were selected"
> > GoTo ExitHandler
> > End If
> >
> >
> > for x = 1 to UBound(FilesToOpen)
> > set bk = Workbooks.Open(FileName:=FilesToOpen(x))
> > bk.Sheets(1).copy After:=ThisWorkbook.Sheets _
> > (ThisWorkbook.Sheets.Count)
> >
> > bk.close saveChanges:=False
> > 'optional
> > FilesToOPen(x).Kill
> > Wend
> >
> > ExitHandler:
> > Application.ScreenUpdating = True
> > Exit Sub
> >
> > ErrHandler:
> > MsgBox Err.Description
> > Resume ExitHandler
> > End Sub
> >
> > --
> > Regards,
> > Tom Ogilvy
> >
> > "Betty" wrote:
> >
> > > I have several (24 in total) workbooks and each workbook only contains one
> > > worksheet and I think that's the problem.
> > >
> > > Thanks
> > >
> > > "Tom Ogilvy" wrote:
> > >
> > > > What are you trying to accomplish here:
> > > > Sheets().Move After:=ThisWorkbook.Sheets _
> > > > (ThisWorkbook.Sheets.Count)
> > > >
> > > >
> > > > Sheets() is a problem. You have to specify which sheet you want to move.
> > > >
> > > > perhaps
> > > >
> > > > Activesheet.Move After:=ThisWorkbook.Sheets _
> > > > (ThisWorkbook.Sheets.Count)
> > > >
> > > >
> > > > Although I if there is only one sheet in the workbook, then I don't believe
> > > > you can move it to another workbook. Perhaps use copy in that case and
> > > > delete all the workbooks after.
> > > >
> > > > --
> > > > Regards,
> > > > Tom Ogilvy
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > "Betty" wrote:
> > > >
> > > > > While trying to run this macro, I get this error. Any insights?
> > > > >
> > > > > Thanks
> > > > > ---------------------------
> > > > > Microsoft Excel
> > > > > ---------------------------
> > > > > Method 'Move' of object 'Sheets' failed
> > > > > ---------------------------
> > > > > OK
> > > > > ---------------------------
> > > > >
> > > > > Here is the Syntax
> > > > >
> > > > > Sub CombineWorkbooks()
> > > > > Dim FilesToOpen
> > > > > Dim x As Integer
> > > > >
> > > > > On Error GoTo ErrHandler
> > > > > Application.ScreenUpdating = False
> > > > >
> > > > > FilesToOpen = Application.GetOpenFilename _
> > > > > (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
> > > > > MultiSelect:=True, Title:="Files to Merge")
> > > > >
> > > > > If TypeName(FilesToOpen) = "Boolean" Then
> > > > > MsgBox "No Files were selected"
> > > > > GoTo ExitHandler
> > > > > End If
> > > > >
> > > > > x = 1
> > > > > While x <= UBound(FilesToOpen)
> > > > > Workbooks.Open FileName:=FilesToOpen(x)
> > > > > Sheets().Move After:=ThisWorkbook.Sheets _
> > > > > (ThisWorkbook.Sheets.Count)
> > > > > x = x + 1
> > > > > Wend
> > > > >
> > > > > ExitHandler:
> > > > > Application.ScreenUpdating = True
> > > > > Exit Sub
> > > > >
> > > > > ErrHandler:
> > > > > MsgBox Err.Description
> > > > > Resume ExitHandler
> > > > > End Sub
> > > > >

 
Reply With Quote
 
=?Utf-8?B?VG9tIE9naWx2eQ==?=
Guest
Posts: n/a
 
      28th Mar 2007
What are you trying to accomplish here:
Sheets().Move After:=ThisWorkbook.Sheets _
(ThisWorkbook.Sheets.Count)


Sheets() is a problem. You have to specify which sheet you want to move.

perhaps

Activesheet.Move After:=ThisWorkbook.Sheets _
(ThisWorkbook.Sheets.Count)


Although I if there is only one sheet in the workbook, then I don't believe
you can move it to another workbook. Perhaps use copy in that case and
delete all the workbooks after.

--
Regards,
Tom Ogilvy





"Betty" wrote:

> While trying to run this macro, I get this error. Any insights?
>
> Thanks
> ---------------------------
> Microsoft Excel
> ---------------------------
> Method 'Move' of object 'Sheets' failed
> ---------------------------
> OK
> ---------------------------
>
> Here is the Syntax
>
> Sub CombineWorkbooks()
> Dim FilesToOpen
> Dim x As Integer
>
> On Error GoTo ErrHandler
> Application.ScreenUpdating = False
>
> FilesToOpen = Application.GetOpenFilename _
> (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
> MultiSelect:=True, Title:="Files to Merge")
>
> If TypeName(FilesToOpen) = "Boolean" Then
> MsgBox "No Files were selected"
> GoTo ExitHandler
> End If
>
> x = 1
> While x <= UBound(FilesToOpen)
> Workbooks.Open FileName:=FilesToOpen(x)
> Sheets().Move After:=ThisWorkbook.Sheets _
> (ThisWorkbook.Sheets.Count)
> x = x + 1
> Wend
>
> ExitHandler:
> Application.ScreenUpdating = True
> Exit Sub
>
> ErrHandler:
> MsgBox Err.Description
> Resume ExitHandler
> End Sub
>

 
Reply With Quote
 
=?Utf-8?B?QmV0dHk=?=
Guest
Posts: n/a
 
      28th Mar 2007
Yes I did.
Thank you so much for your help.


"Tom Ogilvy" wrote:

> Comment out your error handler so you can see what the situation is. When
> you get the error message, choose debug.
>
> then see where it stops and what the value of your variables are.
>
> It worked for me with vanilla workbooks. I saw problems with workbooks
> that had links and workbooks that were password protected.
>
> Anyway, the basic code appears to be sound and no you have to deal with
> peculiarities/special situations in your workbooks.
>
> (I assume you replaced the Wend with a Next. )
>
> --
> Regards,
> Tom Ogilvy
>
>
> "Betty" wrote:
>
> > This is the error that I'm getting now
> >
> > ---------------------------
> > Microsoft Excel
> > ---------------------------
> > Copy method of Worksheet class failed
> > ---------------------------
> > OK
> > ---------------------------
> >
> >
> > "Tom Ogilvy" wrote:
> >
> > > Sub CombineWorkbooks()
> > > Dim FilesToOpen
> > > Dim x As Integer
> > >
> > > On Error GoTo ErrHandler
> > > Application.ScreenUpdating = False
> > >
> > > FilesToOpen = Application.GetOpenFilename _
> > > (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
> > > MultiSelect:=True, Title:="Files to Merge")
> > >
> > > If TypeName(FilesToOpen) = "Boolean" Then
> > > MsgBox "No Files were selected"
> > > GoTo ExitHandler
> > > End If
> > >
> > >
> > > for x = 1 to UBound(FilesToOpen)
> > > set bk = Workbooks.Open(FileName:=FilesToOpen(x))
> > > bk.Sheets(1).copy After:=ThisWorkbook.Sheets _
> > > (ThisWorkbook.Sheets.Count)
> > >
> > > bk.close saveChanges:=False
> > > 'optional
> > > FilesToOPen(x).Kill
> > > Wend
> > >
> > > ExitHandler:
> > > Application.ScreenUpdating = True
> > > Exit Sub
> > >
> > > ErrHandler:
> > > MsgBox Err.Description
> > > Resume ExitHandler
> > > End Sub
> > >
> > > --
> > > Regards,
> > > Tom Ogilvy
> > >
> > > "Betty" wrote:
> > >
> > > > I have several (24 in total) workbooks and each workbook only contains one
> > > > worksheet and I think that's the problem.
> > > >
> > > > Thanks
> > > >
> > > > "Tom Ogilvy" wrote:
> > > >
> > > > > What are you trying to accomplish here:
> > > > > Sheets().Move After:=ThisWorkbook.Sheets _
> > > > > (ThisWorkbook.Sheets.Count)
> > > > >
> > > > >
> > > > > Sheets() is a problem. You have to specify which sheet you want to move.
> > > > >
> > > > > perhaps
> > > > >
> > > > > Activesheet.Move After:=ThisWorkbook.Sheets _
> > > > > (ThisWorkbook.Sheets.Count)
> > > > >
> > > > >
> > > > > Although I if there is only one sheet in the workbook, then I don't believe
> > > > > you can move it to another workbook. Perhaps use copy in that case and
> > > > > delete all the workbooks after.
> > > > >
> > > > > --
> > > > > Regards,
> > > > > Tom Ogilvy
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > "Betty" wrote:
> > > > >
> > > > > > While trying to run this macro, I get this error. Any insights?
> > > > > >
> > > > > > Thanks
> > > > > > ---------------------------
> > > > > > Microsoft Excel
> > > > > > ---------------------------
> > > > > > Method 'Move' of object 'Sheets' failed
> > > > > > ---------------------------
> > > > > > OK
> > > > > > ---------------------------
> > > > > >
> > > > > > Here is the Syntax
> > > > > >
> > > > > > Sub CombineWorkbooks()
> > > > > > Dim FilesToOpen
> > > > > > Dim x As Integer
> > > > > >
> > > > > > On Error GoTo ErrHandler
> > > > > > Application.ScreenUpdating = False
> > > > > >
> > > > > > FilesToOpen = Application.GetOpenFilename _
> > > > > > (FileFilter:="Microsoft Excel Files (*.xls), *.xls", _
> > > > > > MultiSelect:=True, Title:="Files to Merge")
> > > > > >
> > > > > > If TypeName(FilesToOpen) = "Boolean" Then
> > > > > > MsgBox "No Files were selected"
> > > > > > GoTo ExitHandler
> > > > > > End If
> > > > > >
> > > > > > x = 1
> > > > > > While x <= UBound(FilesToOpen)
> > > > > > Workbooks.Open FileName:=FilesToOpen(x)
> > > > > > Sheets().Move After:=ThisWorkbook.Sheets _
> > > > > > (ThisWorkbook.Sheets.Count)
> > > > > > x = x + 1
> > > > > > Wend
> > > > > >
> > > > > > ExitHandler:
> > > > > > Application.ScreenUpdating = True
> > > > > > Exit Sub
> > > > > >
> > > > > > ErrHandler:
> > > > > > MsgBox Err.Description
> > > > > > Resume ExitHandler
> > > > > > End Sub
> > > > > >

 
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
Combining Workbooks =?Utf-8?B?Um9ubmllRg==?= Microsoft Excel Charting 0 17th May 2007 08:40 PM
RE: Combining several workbooks into one workbook =?Utf-8?B?QmV0dHk=?= Microsoft Excel Programming 2 28th Mar 2007 12:22 AM
Combining Workbooks ceecil Microsoft Excel Worksheet Functions 1 1st Feb 2007 04:14 AM
combining workbooks Fawn Microsoft Excel Worksheet Functions 0 13th Apr 2005 03:24 AM
combining workbooks into a single workbook psgreen Microsoft Excel New Users 2 29th Jun 2004 04:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:40 PM.