Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
hi all.
I've modifed an existing macro to copy renamed worksheets in to a secondary workbook. I've read the KB file from MS that Jim Thomlinson has posted regarding this issue, and if I understand it correctly the problem mainly occurs when the user seeks to copy a worksheet in to the same workbook. My goal is to copy the worksheet in to another, existing workbook. wks.Copy after:=Workbooks("TROABook-200-299.xlsx").Sheets.count the response I get from VBA is a 1004 error, stating that the copy method failed. My intention is to copy the renamed worksheets into a different workbook than they are originally in, and at the end. What am I doing wrong here...? Thank you. Your helps are appreciated. SteveB. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Replace that line with this.
wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) The current line is returning a number. VBA needs to know what that number means. So you need to tell VBA that it is a Sheet Index Number. For example, Sheets(1) or Sheets(5). Hope this helps! If so, let me know by clicking "YES" below. -- Cheers, Ryan "SteveDB1" wrote: hi all. I've modifed an existing macro to copy renamed worksheets in to a secondary workbook. I've read the KB file from MS that Jim Thomlinson has posted regarding this issue, and if I understand it correctly the problem mainly occurs when the user seeks to copy a worksheet in to the same workbook. My goal is to copy the worksheet in to another, existing workbook. wks.Copy after:=Workbooks("TROABook-200-299.xlsx").Sheets.count the response I get from VBA is a 1004 error, stating that the copy method failed. My intention is to copy the renamed worksheets into a different workbook than they are originally in, and at the end. What am I doing wrong here...? Thank you. Your helps are appreciated. SteveB. |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Ryan,
I'm using the code you'd previously given me from this morning's post. I thought that I could place that line of code immediately beneath the rename element-- as follows: ----------------------------------------------------------------------------------------- Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" wks.Name = "Sum-" & strWbkName wks.Copy after:=Sheets(Workbooks("TROABook-200-_ 299.xlsx").Sheets.count) Case "APN" wks.Name = "APN-" & strWbkName wks.Copy after:=Sheets(Workbooks("TROABook-200-_ 299.xlsx").Sheets.count) End Select ------------------------------------------------------------------------------- What did I miss here? Thank you. "RyanH" wrote: Replace that line with this. wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) The current line is returning a number. VBA needs to know what that number means. So you need to tell VBA that it is a Sheet Index Number. For example, Sheets(1) or Sheets(5). Hope this helps! If so, let me know by clicking "YES" below. -- Cheers, Ryan "SteveDB1" wrote: hi all. I've modifed an existing macro to copy renamed worksheets in to a secondary workbook. I've read the KB file from MS that Jim Thomlinson has posted regarding this issue, and if I understand it correctly the problem mainly occurs when the user seeks to copy a worksheet in to the same workbook. My goal is to copy the worksheet in to another, existing workbook. wks.Copy after:=Workbooks("TROABook-200-299.xlsx").Sheets.count the response I get from VBA is a 1004 error, stating that the copy method failed. My intention is to copy the renamed worksheets into a different workbook than they are originally in, and at the end. What am I doing wrong here...? Thank you. Your helps are appreciated. SteveB. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Dim newWB as workbook
Set newWB = Workbooks("TROABook-200-299") myws.Copy after:=newWB.Worksheets(newWB.Worksheets.Count) -- HTH, Barb Reinhardt "SteveDB1" wrote: hi all. I've modifed an existing macro to copy renamed worksheets in to a secondary workbook. I've read the KB file from MS that Jim Thomlinson has posted regarding this issue, and if I understand it correctly the problem mainly occurs when the user seeks to copy a worksheet in to the same workbook. My goal is to copy the worksheet in to another, existing workbook. wks.Copy after:=Workbooks("TROABook-200-299.xlsx").Sheets.count the response I get from VBA is a 1004 error, stating that the copy method failed. My intention is to copy the renamed worksheets into a different workbook than they are originally in, and at the end. What am I doing wrong here...? Thank you. Your helps are appreciated. SteveB. |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Barb, and Ryan,
Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) ......:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Sub copyshttoanotherwb()
Dim dwb As Workbook Dim wks As Worksheet Set dwb = Workbooks("MENU.xls") Set wks = Sheets("Data") With dwb wks.Copy after:=.Sheets(.Sheets.Count) End With End Sub -- Don Guillett Microsoft MVP Excel SalesAid Software "SteveDB1" wrote in message ... Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Is the Workbooks("TROABook-200-299.xlsx") open? If it is not open you will
get that error. Can you post the rest of the code? This will help me to diagnose things. -- Cheers, Ryan "SteveDB1" wrote: Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Yes, the final destination workbook is open.
The entirety of the code is: ------------------------------------------------------------ Sub A1ReNmWksht() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name- minus file extension strWbkName = Mid(ActiveWorkbook.Name, 5, InStr(ActiveWorkbook.Name, ".") - 5) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" wks.Name = "Sum-" & strWbkName wks.Copy after:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) Case "APN" wks.Name = "APN-" & strWbkName wks.Copy after:=Worksheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) End Select Next wks End Sub ------------------------------------------------------------- Again, thank you. "RyanH" wrote: Is the Workbooks("TROABook-200-299.xlsx") open? If it is not open you will get that error. Can you post the rest of the code? This will help me to diagnose things. -- Cheers, Ryan "SteveDB1" wrote: Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I was getting an error on your strWbkName line. Are you? Give this a try:
Sub A1ReNmWksht() Dim strWbkName As String Dim wbk As Workbook Dim wks As Worksheet Dim lngLastSheet As Long ' get last 3 characters of the workbook name- minus file extension strWbkName = Mid(ActiveWorkbook.Name, 5, InStr(ActiveWorkbook.Name, ".") - 5) ' where you want the data to go Set wbk = Workbooks("TROABook-200-299.xlsx") lngLastSheet = wbk.Sheets.Count ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" With wks .Name = "Sum-" & strWbkName .Copy After:=wbk.Sheets(lngLastSheet) End With Case "APN" With wks .Name = "APN-" & strWbkName .Copy After:=wbk.Sheets(lngLastSheet) End With End Select Next wks End Sub -- Cheers, Ryan "SteveDB1" wrote: Yes, the final destination workbook is open. The entirety of the code is: ------------------------------------------------------------ Sub A1ReNmWksht() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name- minus file extension strWbkName = Mid(ActiveWorkbook.Name, 5, InStr(ActiveWorkbook.Name, ".") - 5) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" wks.Name = "Sum-" & strWbkName wks.Copy after:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) Case "APN" wks.Name = "APN-" & strWbkName wks.Copy after:=Worksheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) End Select Next wks End Sub ------------------------------------------------------------- Again, thank you. "RyanH" wrote: Is the Workbooks("TROABook-200-299.xlsx") open? If it is not open you will get that error. Can you post the rest of the code? This will help me to diagnose things. -- Cheers, Ryan "SteveDB1" wrote: Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Morning Ryan.
That was it. It works great now. THANK YOU!!!! yes, I was getting the same error. I did try the with, but I did with selection, not with wks. Again-- thank you for your helps. And to Barb, Don, Dave-- thank you all for your helps. Best to you all........ SteveB. "RyanH" wrote: I was getting an error on your strWbkName line. Are you? Give this a try: Sub A1ReNmWksht() Dim strWbkName As String Dim wbk As Workbook Dim wks As Worksheet Dim lngLastSheet As Long ' get last 3 characters of the workbook name- minus file extension strWbkName = Mid(ActiveWorkbook.Name, 5, InStr(ActiveWorkbook.Name, ".") - 5) ' where you want the data to go Set wbk = Workbooks("TROABook-200-299.xlsx") lngLastSheet = wbk.Sheets.Count ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" With wks .Name = "Sum-" & strWbkName .Copy After:=wbk.Sheets(lngLastSheet) End With Case "APN" With wks .Name = "APN-" & strWbkName .Copy After:=wbk.Sheets(lngLastSheet) End With End Select Next wks End Sub -- Cheers, Ryan "SteveDB1" wrote: Yes, the final destination workbook is open. The entirety of the code is: ------------------------------------------------------------ Sub A1ReNmWksht() Dim strWbkName As String Dim wks As Worksheet ' get last 3 characters of the workbook name- minus file extension strWbkName = Mid(ActiveWorkbook.Name, 5, InStr(ActiveWorkbook.Name, ".") - 5) ' find worksheet name and change worksheet name if found For Each wks In ActiveWorkbook.Worksheets Select Case wks.Name Case "Sum", "Summary", "SUM", "summary" wks.Name = "Sum-" & strWbkName wks.Copy after:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) Case "APN" wks.Name = "APN-" & strWbkName wks.Copy after:=Worksheets(Workbooks("TROABook-200-299.xlsx").Sheets.count) End Select Next wks End Sub ------------------------------------------------------------- Again, thank you. "RyanH" wrote: Is the Workbooks("TROABook-200-299.xlsx") open? If it is not open you will get that error. Can you post the rest of the code? This will help me to diagnose things. -- Cheers, Ryan "SteveDB1" wrote: Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'd include the extension in Barb's code:
Set newWB = Workbooks("TROABook-200-299.xlsx") RyanH wrote: Is the Workbooks("TROABook-200-299.xlsx") open? If it is not open you will get that error. Can you post the rest of the code? This will help me to diagnose things. -- Cheers, Ryan "SteveDB1" wrote: Barb, and Ryan, Thank you both for your responses. I've tried both and get the same error. subscript out of range. For Ryan's, the error occurs with the wks.Copy After:=Sheets(Workbooks("TROABook-200-299.xlsx").Sheets.Count) .....:= sheets(...) component. the line shows 23 sheets, within the (Workbooks(...).sheets.count element, but once it is "outside" with the sheets(....) portion it gives the subscript out of range error. For Barb's, the error occurs Set newWB = Workbooks("TROABook-200-299") workbooks(...) = <subscript out of range component. Barb, what if, instead of a new workbook, it just sets the TROABook as being active? I copied your forms and pasted each-- to make sure I didn't mis-type anything. Again-- thank you both. -- Dave Peterson |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I'd use something like this
myws.Copy after:=newWB.Worksheets(newWB.Worksheets.Count) -- HTH, Barb Reinhardt "SteveDB1" wrote: hi all. I've modifed an existing macro to copy renamed worksheets in to a secondary workbook. I've read the KB file from MS that Jim Thomlinson has posted regarding this issue, and if I understand it correctly the problem mainly occurs when the user seeks to copy a worksheet in to the same workbook. My goal is to copy the worksheet in to another, existing workbook. wks.Copy after:=Workbooks("TROABook-200-299.xlsx").Sheets.count the response I get from VBA is a 1004 error, stating that the copy method failed. My intention is to copy the renamed worksheets into a different workbook than they are originally in, and at the end. What am I doing wrong here...? Thank you. Your helps are appreciated. SteveB. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Method 'Copy' of object'_Worksheet failed | Excel Worksheet Functions | |||
Method 'Copy' of object'_Worksheet failed | Excel Programming | |||
Copy method failed | Excel Programming | |||
Copy Method of Worksheet Class Failed | Excel Programming | |||
Copy Method of Worksheet Class Failed | Excel Programming |