PC Review


Reply
Thread Tools Rate Thread

Copy method of Worksheet class failed

 
 
=?Utf-8?B?Q2hhcGxhaW4gRG91Zw==?=
Guest
Posts: n/a
 
      15th Mar 2007
Excel 2003. I have code in an Excel workbook that has worked forever. Now I
am getting the error:

Copy method of Worksheet class failed

In the line below that is ********ed. It successfully copies dozens of
sheets and then begins to fail. The sheet on which it fails is not
protected, nor is the workbook protected. Something just gets grunged in the
workbook. If I close the books and then open them both manually and do the
copy manually, it works fine. What might be the problem?

Also, I also have Excel 2007 installed on this machine. But I am using
Excel 2003 when this problem occurs. Thanks for any help.

Sub Create_YTD_Master_Workbook(YTDBook As String, strYear As String, Success
As Boolean)
Dim MBSearch As String, NextWb As String
Dim I As Integer, J As Integer, NFiles As Integer, JJ As Integer
Dim YTDWbs(1 To 12) As String
Dim YTDWb As Workbook, TempWb As Workbook, sh As Worksheet
Dim YTDWbName As String, Monthstr As String, shName As String
Dim FromRange As Range, ToRange As Range
On Error GoTo CYW_Err
Application.ScreenUpdating = False
'Get monthly master file names in order
Call Get_Monthly_Masters(YTDWbs, strYear, NFiles)
'Create YTD master from monthly books
If NFiles > 0 Then
Application.SheetsInNewWorkbook = 1
Set YTDWb = Workbooks.Add
YTDWbName = strYear + " YTD Master.xls"
Application.DisplayAlerts = False
YTDWb.SaveAs YTDWbName
YTDBook = YTDWb.FullName 'For pass back to calling routine
'Copy first monthly workbook to YTD Master
Call UpdateStatus("Processing " + YTDWbs(1))
Set TempWb = Workbooks.Open(YTDWbs(1), , ReadOnly:=True)
TempWb.Sheets.Copy After:=YTDWb.Sheets(1)
YTDWb.Sheets("Sheet1").Delete 'Remove Sheet 1
TempWb.Close
Application.DisplayAlerts = True
For J = 2 To NFiles
Call UpdateStatus("Processing " + YTDWbs(J))
Set TempWb = Workbooks.Open(YTDWbs(J), , ReadOnly:=True)
TempWb.Unprotect
For I = 1 To TempWb.Sheets.Count
DoEvents
shName = TempWb.Sheets(I).Name
If SheetExists(shName, YTDWb) Then 'Copy the contents onto
existing sheet
JJ = LastRow(YTDWb.Sheets(shName)) + 1
YTDWb.Sheets(shName).Unprotect
TempWb.Sheets(I).UsedRange.Copy
Destination:=YTDWb.Sheets(shName).Range("A" + CStr(JJ))
YTDWb.Sheets(shName).Protect
ElseIf InStr(shName, "Sheet") = 0 Then 'Copy as new sheet
JJ = YTDWb.Sheets.Count
******** TempWb.Sheets(I).Copy After:=YTDWb.Sheets(JJ)
Debug.Print I, JJ, TempWb.Sheets(I).Name
YTDWb.Unprotect
End If
'Remove formulas and replace with fixed values
shName = TempWb.Sheets(I).Name
'Call RemoveFormulas(YTDWb.Sheets(shName))
Next I
'Remove cell names from workbook
Call RemoveNames(YTDWb)
TempWb.Protect
TempWb.Close
Next J
Call RemoveLinks(YTDWb)
Call SortWorkbookSheets(YTDWb)
YTDWb.Save
YTDWb.Protect
YTDWb.Close 'SaveChanges:=True
Success = True
Else
Success = False
End If
CYW_Exit:
Application.ScreenUpdating = True
Exit Sub
CYW_Err:
Resume Next
Success = False
GoTo CYW_Exit
End Sub
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      15th Mar 2007
check out this link...

http://support.microsoft.com/default...84&Product=xlw
--
HTH...

Jim Thomlinson


"Chaplain Doug" wrote:

> Excel 2003. I have code in an Excel workbook that has worked forever. Now I
> am getting the error:
>
> Copy method of Worksheet class failed
>
> In the line below that is ********ed. It successfully copies dozens of
> sheets and then begins to fail. The sheet on which it fails is not
> protected, nor is the workbook protected. Something just gets grunged in the
> workbook. If I close the books and then open them both manually and do the
> copy manually, it works fine. What might be the problem?
>
> Also, I also have Excel 2007 installed on this machine. But I am using
> Excel 2003 when this problem occurs. Thanks for any help.
>
> Sub Create_YTD_Master_Workbook(YTDBook As String, strYear As String, Success
> As Boolean)
> Dim MBSearch As String, NextWb As String
> Dim I As Integer, J As Integer, NFiles As Integer, JJ As Integer
> Dim YTDWbs(1 To 12) As String
> Dim YTDWb As Workbook, TempWb As Workbook, sh As Worksheet
> Dim YTDWbName As String, Monthstr As String, shName As String
> Dim FromRange As Range, ToRange As Range
> On Error GoTo CYW_Err
> Application.ScreenUpdating = False
> 'Get monthly master file names in order
> Call Get_Monthly_Masters(YTDWbs, strYear, NFiles)
> 'Create YTD master from monthly books
> If NFiles > 0 Then
> Application.SheetsInNewWorkbook = 1
> Set YTDWb = Workbooks.Add
> YTDWbName = strYear + " YTD Master.xls"
> Application.DisplayAlerts = False
> YTDWb.SaveAs YTDWbName
> YTDBook = YTDWb.FullName 'For pass back to calling routine
> 'Copy first monthly workbook to YTD Master
> Call UpdateStatus("Processing " + YTDWbs(1))
> Set TempWb = Workbooks.Open(YTDWbs(1), , ReadOnly:=True)
> TempWb.Sheets.Copy After:=YTDWb.Sheets(1)
> YTDWb.Sheets("Sheet1").Delete 'Remove Sheet 1
> TempWb.Close
> Application.DisplayAlerts = True
> For J = 2 To NFiles
> Call UpdateStatus("Processing " + YTDWbs(J))
> Set TempWb = Workbooks.Open(YTDWbs(J), , ReadOnly:=True)
> TempWb.Unprotect
> For I = 1 To TempWb.Sheets.Count
> DoEvents
> shName = TempWb.Sheets(I).Name
> If SheetExists(shName, YTDWb) Then 'Copy the contents onto
> existing sheet
> JJ = LastRow(YTDWb.Sheets(shName)) + 1
> YTDWb.Sheets(shName).Unprotect
> TempWb.Sheets(I).UsedRange.Copy
> Destination:=YTDWb.Sheets(shName).Range("A" + CStr(JJ))
> YTDWb.Sheets(shName).Protect
> ElseIf InStr(shName, "Sheet") = 0 Then 'Copy as new sheet
> JJ = YTDWb.Sheets.Count
> ******** TempWb.Sheets(I).Copy After:=YTDWb.Sheets(JJ)
> Debug.Print I, JJ, TempWb.Sheets(I).Name
> YTDWb.Unprotect
> End If
> 'Remove formulas and replace with fixed values
> shName = TempWb.Sheets(I).Name
> 'Call RemoveFormulas(YTDWb.Sheets(shName))
> Next I
> 'Remove cell names from workbook
> Call RemoveNames(YTDWb)
> TempWb.Protect
> TempWb.Close
> Next J
> Call RemoveLinks(YTDWb)
> Call SortWorkbookSheets(YTDWb)
> YTDWb.Save
> YTDWb.Protect
> YTDWb.Close 'SaveChanges:=True
> Success = True
> Else
> Success = False
> End If
> CYW_Exit:
> Application.ScreenUpdating = True
> Exit Sub
> CYW_Err:
> Resume Next
> Success = False
> GoTo CYW_Exit
> End Sub
> --
> Dr. Doug Pruiett
> Good News Jail & Prison Ministry
> www.goodnewsjail.org

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      15th Mar 2007
Give this a whirl...

Public Function LetterToNumber(ByVal Letter As String)
LetterToNumber = Asc(UCase(Letter)) - 64
End Function

Sub test()
MsgBox LetterToNumber("A")
MsgBox LetterToNumber("r")
End Sub

--
HTH...

Jim Thomlinson


"Chaplain Doug" wrote:

> Excel 2003. I have code in an Excel workbook that has worked forever. Now I
> am getting the error:
>
> Copy method of Worksheet class failed
>
> In the line below that is ********ed. It successfully copies dozens of
> sheets and then begins to fail. The sheet on which it fails is not
> protected, nor is the workbook protected. Something just gets grunged in the
> workbook. If I close the books and then open them both manually and do the
> copy manually, it works fine. What might be the problem?
>
> Also, I also have Excel 2007 installed on this machine. But I am using
> Excel 2003 when this problem occurs. Thanks for any help.
>
> Sub Create_YTD_Master_Workbook(YTDBook As String, strYear As String, Success
> As Boolean)
> Dim MBSearch As String, NextWb As String
> Dim I As Integer, J As Integer, NFiles As Integer, JJ As Integer
> Dim YTDWbs(1 To 12) As String
> Dim YTDWb As Workbook, TempWb As Workbook, sh As Worksheet
> Dim YTDWbName As String, Monthstr As String, shName As String
> Dim FromRange As Range, ToRange As Range
> On Error GoTo CYW_Err
> Application.ScreenUpdating = False
> 'Get monthly master file names in order
> Call Get_Monthly_Masters(YTDWbs, strYear, NFiles)
> 'Create YTD master from monthly books
> If NFiles > 0 Then
> Application.SheetsInNewWorkbook = 1
> Set YTDWb = Workbooks.Add
> YTDWbName = strYear + " YTD Master.xls"
> Application.DisplayAlerts = False
> YTDWb.SaveAs YTDWbName
> YTDBook = YTDWb.FullName 'For pass back to calling routine
> 'Copy first monthly workbook to YTD Master
> Call UpdateStatus("Processing " + YTDWbs(1))
> Set TempWb = Workbooks.Open(YTDWbs(1), , ReadOnly:=True)
> TempWb.Sheets.Copy After:=YTDWb.Sheets(1)
> YTDWb.Sheets("Sheet1").Delete 'Remove Sheet 1
> TempWb.Close
> Application.DisplayAlerts = True
> For J = 2 To NFiles
> Call UpdateStatus("Processing " + YTDWbs(J))
> Set TempWb = Workbooks.Open(YTDWbs(J), , ReadOnly:=True)
> TempWb.Unprotect
> For I = 1 To TempWb.Sheets.Count
> DoEvents
> shName = TempWb.Sheets(I).Name
> If SheetExists(shName, YTDWb) Then 'Copy the contents onto
> existing sheet
> JJ = LastRow(YTDWb.Sheets(shName)) + 1
> YTDWb.Sheets(shName).Unprotect
> TempWb.Sheets(I).UsedRange.Copy
> Destination:=YTDWb.Sheets(shName).Range("A" + CStr(JJ))
> YTDWb.Sheets(shName).Protect
> ElseIf InStr(shName, "Sheet") = 0 Then 'Copy as new sheet
> JJ = YTDWb.Sheets.Count
> ******** TempWb.Sheets(I).Copy After:=YTDWb.Sheets(JJ)
> Debug.Print I, JJ, TempWb.Sheets(I).Name
> YTDWb.Unprotect
> End If
> 'Remove formulas and replace with fixed values
> shName = TempWb.Sheets(I).Name
> 'Call RemoveFormulas(YTDWb.Sheets(shName))
> Next I
> 'Remove cell names from workbook
> Call RemoveNames(YTDWb)
> TempWb.Protect
> TempWb.Close
> Next J
> Call RemoveLinks(YTDWb)
> Call SortWorkbookSheets(YTDWb)
> YTDWb.Save
> YTDWb.Protect
> YTDWb.Close 'SaveChanges:=True
> Success = True
> Else
> Success = False
> End If
> CYW_Exit:
> Application.ScreenUpdating = True
> Exit Sub
> CYW_Err:
> Resume Next
> Success = False
> GoTo CYW_Exit
> End Sub
> --
> Dr. Doug Pruiett
> Good News Jail & Prison Ministry
> www.goodnewsjail.org

 
Reply With Quote
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      15th Mar 2007
Ooops... Wrong Thread...
--
HTH...

Jim Thomlinson


"Jim Thomlinson" wrote:

> Give this a whirl...
>
> Public Function LetterToNumber(ByVal Letter As String)
> LetterToNumber = Asc(UCase(Letter)) - 64
> End Function
>
> Sub test()
> MsgBox LetterToNumber("A")
> MsgBox LetterToNumber("r")
> End Sub
>
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Chaplain Doug" wrote:
>
> > Excel 2003. I have code in an Excel workbook that has worked forever. Now I
> > am getting the error:
> >
> > Copy method of Worksheet class failed
> >
> > In the line below that is ********ed. It successfully copies dozens of
> > sheets and then begins to fail. The sheet on which it fails is not
> > protected, nor is the workbook protected. Something just gets grunged in the
> > workbook. If I close the books and then open them both manually and do the
> > copy manually, it works fine. What might be the problem?
> >
> > Also, I also have Excel 2007 installed on this machine. But I am using
> > Excel 2003 when this problem occurs. Thanks for any help.
> >
> > Sub Create_YTD_Master_Workbook(YTDBook As String, strYear As String, Success
> > As Boolean)
> > Dim MBSearch As String, NextWb As String
> > Dim I As Integer, J As Integer, NFiles As Integer, JJ As Integer
> > Dim YTDWbs(1 To 12) As String
> > Dim YTDWb As Workbook, TempWb As Workbook, sh As Worksheet
> > Dim YTDWbName As String, Monthstr As String, shName As String
> > Dim FromRange As Range, ToRange As Range
> > On Error GoTo CYW_Err
> > Application.ScreenUpdating = False
> > 'Get monthly master file names in order
> > Call Get_Monthly_Masters(YTDWbs, strYear, NFiles)
> > 'Create YTD master from monthly books
> > If NFiles > 0 Then
> > Application.SheetsInNewWorkbook = 1
> > Set YTDWb = Workbooks.Add
> > YTDWbName = strYear + " YTD Master.xls"
> > Application.DisplayAlerts = False
> > YTDWb.SaveAs YTDWbName
> > YTDBook = YTDWb.FullName 'For pass back to calling routine
> > 'Copy first monthly workbook to YTD Master
> > Call UpdateStatus("Processing " + YTDWbs(1))
> > Set TempWb = Workbooks.Open(YTDWbs(1), , ReadOnly:=True)
> > TempWb.Sheets.Copy After:=YTDWb.Sheets(1)
> > YTDWb.Sheets("Sheet1").Delete 'Remove Sheet 1
> > TempWb.Close
> > Application.DisplayAlerts = True
> > For J = 2 To NFiles
> > Call UpdateStatus("Processing " + YTDWbs(J))
> > Set TempWb = Workbooks.Open(YTDWbs(J), , ReadOnly:=True)
> > TempWb.Unprotect
> > For I = 1 To TempWb.Sheets.Count
> > DoEvents
> > shName = TempWb.Sheets(I).Name
> > If SheetExists(shName, YTDWb) Then 'Copy the contents onto
> > existing sheet
> > JJ = LastRow(YTDWb.Sheets(shName)) + 1
> > YTDWb.Sheets(shName).Unprotect
> > TempWb.Sheets(I).UsedRange.Copy
> > Destination:=YTDWb.Sheets(shName).Range("A" + CStr(JJ))
> > YTDWb.Sheets(shName).Protect
> > ElseIf InStr(shName, "Sheet") = 0 Then 'Copy as new sheet
> > JJ = YTDWb.Sheets.Count
> > ******** TempWb.Sheets(I).Copy After:=YTDWb.Sheets(JJ)
> > Debug.Print I, JJ, TempWb.Sheets(I).Name
> > YTDWb.Unprotect
> > End If
> > 'Remove formulas and replace with fixed values
> > shName = TempWb.Sheets(I).Name
> > 'Call RemoveFormulas(YTDWb.Sheets(shName))
> > Next I
> > 'Remove cell names from workbook
> > Call RemoveNames(YTDWb)
> > TempWb.Protect
> > TempWb.Close
> > Next J
> > Call RemoveLinks(YTDWb)
> > Call SortWorkbookSheets(YTDWb)
> > YTDWb.Save
> > YTDWb.Protect
> > YTDWb.Close 'SaveChanges:=True
> > Success = True
> > Else
> > Success = False
> > End If
> > CYW_Exit:
> > Application.ScreenUpdating = True
> > Exit Sub
> > CYW_Err:
> > Resume Next
> > Success = False
> > GoTo CYW_Exit
> > End Sub
> > --
> > Dr. Doug Pruiett
> > Good News Jail & Prison Ministry
> > www.goodnewsjail.org

 
Reply With Quote
 
=?Utf-8?B?Q2hhcGxhaW4gRG91Zw==?=
Guest
Posts: n/a
 
      15th Mar 2007
Thank you for the quick reply!!!! I added the periodic save/close/reopen and
now the process completes without error. Thanks and God bless.
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


"Jim Thomlinson" wrote:

> check out this link...
>
> http://support.microsoft.com/default...84&Product=xlw
> --
> HTH...
>
> Jim Thomlinson
>
>
> "Chaplain Doug" wrote:
>
> > Excel 2003. I have code in an Excel workbook that has worked forever. Now I
> > am getting the error:
> >
> > Copy method of Worksheet class failed
> >
> > In the line below that is ********ed. It successfully copies dozens of
> > sheets and then begins to fail. The sheet on which it fails is not
> > protected, nor is the workbook protected. Something just gets grunged in the
> > workbook. If I close the books and then open them both manually and do the
> > copy manually, it works fine. What might be the problem?
> >
> > Also, I also have Excel 2007 installed on this machine. But I am using
> > Excel 2003 when this problem occurs. Thanks for any help.
> >
> > Sub Create_YTD_Master_Workbook(YTDBook As String, strYear As String, Success
> > As Boolean)
> > Dim MBSearch As String, NextWb As String
> > Dim I As Integer, J As Integer, NFiles As Integer, JJ As Integer
> > Dim YTDWbs(1 To 12) As String
> > Dim YTDWb As Workbook, TempWb As Workbook, sh As Worksheet
> > Dim YTDWbName As String, Monthstr As String, shName As String
> > Dim FromRange As Range, ToRange As Range
> > On Error GoTo CYW_Err
> > Application.ScreenUpdating = False
> > 'Get monthly master file names in order
> > Call Get_Monthly_Masters(YTDWbs, strYear, NFiles)
> > 'Create YTD master from monthly books
> > If NFiles > 0 Then
> > Application.SheetsInNewWorkbook = 1
> > Set YTDWb = Workbooks.Add
> > YTDWbName = strYear + " YTD Master.xls"
> > Application.DisplayAlerts = False
> > YTDWb.SaveAs YTDWbName
> > YTDBook = YTDWb.FullName 'For pass back to calling routine
> > 'Copy first monthly workbook to YTD Master
> > Call UpdateStatus("Processing " + YTDWbs(1))
> > Set TempWb = Workbooks.Open(YTDWbs(1), , ReadOnly:=True)
> > TempWb.Sheets.Copy After:=YTDWb.Sheets(1)
> > YTDWb.Sheets("Sheet1").Delete 'Remove Sheet 1
> > TempWb.Close
> > Application.DisplayAlerts = True
> > For J = 2 To NFiles
> > Call UpdateStatus("Processing " + YTDWbs(J))
> > Set TempWb = Workbooks.Open(YTDWbs(J), , ReadOnly:=True)
> > TempWb.Unprotect
> > For I = 1 To TempWb.Sheets.Count
> > DoEvents
> > shName = TempWb.Sheets(I).Name
> > If SheetExists(shName, YTDWb) Then 'Copy the contents onto
> > existing sheet
> > JJ = LastRow(YTDWb.Sheets(shName)) + 1
> > YTDWb.Sheets(shName).Unprotect
> > TempWb.Sheets(I).UsedRange.Copy
> > Destination:=YTDWb.Sheets(shName).Range("A" + CStr(JJ))
> > YTDWb.Sheets(shName).Protect
> > ElseIf InStr(shName, "Sheet") = 0 Then 'Copy as new sheet
> > JJ = YTDWb.Sheets.Count
> > ******** TempWb.Sheets(I).Copy After:=YTDWb.Sheets(JJ)
> > Debug.Print I, JJ, TempWb.Sheets(I).Name
> > YTDWb.Unprotect
> > End If
> > 'Remove formulas and replace with fixed values
> > shName = TempWb.Sheets(I).Name
> > 'Call RemoveFormulas(YTDWb.Sheets(shName))
> > Next I
> > 'Remove cell names from workbook
> > Call RemoveNames(YTDWb)
> > TempWb.Protect
> > TempWb.Close
> > Next J
> > Call RemoveLinks(YTDWb)
> > Call SortWorkbookSheets(YTDWb)
> > YTDWb.Save
> > YTDWb.Protect
> > YTDWb.Close 'SaveChanges:=True
> > Success = True
> > Else
> > Success = False
> > End If
> > CYW_Exit:
> > Application.ScreenUpdating = True
> > Exit Sub
> > CYW_Err:
> > Resume Next
> > Success = False
> > GoTo CYW_Exit
> > End Sub
> > --
> > Dr. Doug Pruiett
> > Good News Jail & Prison Ministry
> > www.goodnewsjail.org

 
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
Copy method of Worksheet class failed =?Utf-8?B?RnJhbmNpcyBCcm93bg==?= Microsoft Excel Programming 1 26th Oct 2005 08:22 PM
Copy Method of Worksheet Class Failed Steph Microsoft Excel Programming 1 5th Apr 2004 10:15 PM
Copy Method of Worksheet Class Failed Steph Microsoft Excel Programming 1 5th Apr 2004 09:56 PM
Copy Method of Worksheet Class Failed Steph Microsoft Excel Programming 1 5th Apr 2004 09:38 PM
Copy method of Worksheet class failed? Steph Microsoft Excel Programming 1 2nd Apr 2004 09:52 PM


Features
 

Advertising
 

Newsgroups
 


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