-
#2
Not sure why it fails.
Maybe you can try this:
1. Add Debug.Print
VBA Code:
Debug.Print txtClaimsFname
Debug.Print txtClaimsShtName
Debug.Print txtMacroFname
Workbooks(txtClaimsFname).Worksheets(txtClaimsShtName).Copy After:=Workbooks(txtMacroFname).Worksheets("By Market Report")
2. Do the copy manually & record the process by Macro recorder. Maybe you can see something different with the names there.
Fluff
MrExcel MVP, Moderator
-
#3
Do either of the workbooks, have workbook protection set on the Review tab?
Does the macro workbook already have a sheet with the same names as the sheet you are trying to copy?
-
#4
Not sure why it fails.
Maybe you can try this:
1. Add Debug.PrintVBA Code:
Debug.Print txtClaimsFname Debug.Print txtClaimsShtName Debug.Print txtMacroFname Workbooks(txtClaimsFname).Worksheets(txtClaimsShtName).Copy After:=Workbooks(txtMacroFname).Worksheets("By Market Report")
2. Do the copy manually & record the process by Macro recorder. Maybe you can see something different with the names there.
Thank you, Akuini. I have been including debug.print on these variables ahead of the copy code, and the content matches what is displayed when I step through the code and hover over the variables. Thanks for the macro recorder idea. Unfortunately, I tried these combinations; the code chokes at line 2, whether it is an activate or select:
Macro recorder code:
Windows(«Interim v1.2 test claims.xlsx»). Activate
Sheets(«Provider Disruption Report»).Select
Sheets(«Provider Disruption Report»).Copy Before:=Workbooks( _
«VBA DA Template 1.2. adding geo code.xlsm»).Sheets(3)
Windows(«Interim v1.2 test claims.xlsx»). Activate
Sheets(«Geocoded»).Select
Sheets(«Geocoded»).Copy Before:=Workbooks( _
«VBA DA Template 1.2. adding geo code.xlsm»).Sheets(4)
I tried dropping the macro recorder code into my code, and it bombed. That tells me something needs to be edited.
‘The following code causes error: select method of worksheet class failed at line 2,
Windows(txtClaimsFname).Activate
Sheets(txtClaimsShtName).Select
Sheets(txtClaimsShtName).Copy Before:=Workbooks(txtMacroFname).Sheets(3)
Windows(txtClaimsFname).Activate
Sheets(txtGeoClaimsShtName).Select
Sheets(txtGeoClaimsShtName).Copy Before:=Workbooks(txtMacroFname).Sheets(4)
‘Here, I specified the workbook name. Error message: select method of worksheet class failed at Line 2.
Windows(txtClaimsFname).Activate
‘Workbooks(txtClaimsFname).Sheets(txtClaimsShtName).Select
‘Workbooks(txtClaimsFname).Sheets(txtClaimsShtName).Copy Before:=Workbooks(txtMacroFname).Sheets(3)
‘Workbooks(txtClaimsFname).Activate
‘Workbooks(txtClaimsFname).Sheets(«Geocoded»).Select
‘Workbooks(txtClaimsFname).Sheets(«Geocoded»).Copy Before:=Workbooks(txtMacroFname).Sheets(4)
‘ I tried to copy both worksheets. This code creates error message: activate method of worksheet class failed at line 2:
Windows(txtClaimsFname).Activate
Sheets(txtClaimsShtName).Activate
Sheets(Array(txtClaimsShtName, txtGeoClaimsShtName)).Select
Sheets(txtGeoClaimsShtName).Activate
Sheets(Array(txtClaimsShtName, txtGeoClaimsShtName)).Copy Before:=Workbooks(txtMacroFname).Sheets(3)
What is the better way to code this?
-
#5
Do either of the workbooks, have workbook protection set on the Review tab?
Does the macro workbook already have a sheet with the same names as the sheet you are trying to copy?
Hello, Fluff: there is no workbook protection in place in either file. The macro file and the claims file do not share worksheet or worksheet names until the copy code runs.
-
#6
Unfortunately, I tried these combinations; the code chokes at line 2,
It’s weird.
Is it possible that you have trailing space in txtClaimsShtName?
Try this:
VBA Code:
Debug.Print txtClaimsShtName = Trim(txtClaimsShtName)
Debug.Print "Provider Disruption Report" = txtClaimsShtName
Workbooks(txtClaimsFname).Worksheets(txtClaimsShtName).Copy After:=Workbooks(txtMacroFname).Worksheets("By Market Report")
If Debug.Print result is False then there’s your answer.
Last edited: Aug 31, 2021
-
#7
Akuini: Thanks. I ran the 3 lines of code. The middle line shows «true» in the Immediate window. That makes sense because I open the userform as vbmodeless so I could double-click the worksheet name and copy each into the userform. When I run the 3rd line, I still get the Copy method of Worksheet class failed.
-
#8
The middle line shows «true» in the Immediate window.
What about the first line?
Debug.Print txtClaimsShtName = Trim(txtClaimsShtName)
Can you show us part of the code that assign the sheet name to txtClaimsFname?
-
#9
What about the first line?
Debug.Print txtClaimsShtName = Trim(txtClaimsShtName)Can you show us part of the code that assign the sheet name to txtClaimsFname?
My apologies; Both the 1st and 2nd lines return “true.»
The claims file name (txtClaimsFname) is assigned in Sub PostQuestExport1:
With Application.FileDialog(msoFileDialogFilePicker)
.Title = («Find Your HPN Claims File and Click OK»)
‘Makes sure the user can select only one file
.AllowMultiSelect = False
‘Filter to just the following types of files to narrow down selection options
.Filters.Add «Excel Files», «*.xlsx; *.xlsm; *.xls; *.xlsb;*.csv», 1
‘Show the dialog box
‘Store in fullpath variable
If .Show <> 0 Then
txtClaimsFpath = .SelectedItems.Item(1)
txtClaimsFname = Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), «») + 1)
Else
MsgBox («No file was selected. When ready to start again, click the cloud icon.»)
Exit Sub
End If
End With
Sub PostQuestExport1 ends by loading the userform: Form.Show vbModeless
Code from the userform that assigns the worksheet names:
If TextBox6 = «» Then
MsgBox («Paste the claims file tab name here. This field cannot be blank.»)
TextBox6.SetFocus
Exit Sub
Else
txtClaimsShtName = TextBox6.Text
End If
If TextBox7 = «» Then
MsgBox («Paste the Geocoded claims tab name here. This field cannot be blank.»)
TextBox7.SetFocus
Exit Sub
Else
txtGeoClaimsShtName = TextBox7.Text
End If
At the end of this sub, I then pass these values:
Call PostQuestExport2(txtDrive1, txtDiv, CompName, txtClaimsShtName, txtPdColPres, txtPdCol, intHeadrRow, txtGeoClaimsShtName)
Inside PostQuestExport2:
Sub PostQuestExport2(txtDrive1 As String, txtDiv As String, CompName As String, txtClaimsShtName As String, _
txtPdColPres As String, txtPdCol As String, intHeadrRow As Integer, txtGeoClaimsShtName As String)
-
#10
Hi MAM8433. It doesn’t seem like you’re actually opening your selected file. You can trial this code. Please save a back up copy of your wbs before trialing the code. HTH. Dave
Code:
Sub CopySheets()
Dim FileNm As Object, TargetFiles As FileDialog
Dim txtClaimsShtName As String, txtGeoClaimsShtName As String
txtClaimsShtName = TextBox6.Text
txtGeoClaimsShtName = TextBox7.Text
Set TargetFiles = Application.FileDialog(msoFileDialogFilePicker)
With TargetFiles
.Title = ("Find Your HPN Claims File and Click OK")
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb;*.csv", 1
If .Show = 0 Then
MsgBox ("No file was selected. When ready to start again, click the cloud icon.")
Exit Sub
End If
End With
On Error GoTo below
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'open the file and assign the workbook
Set FileNm = Workbooks.Open(TargetFiles.SelectedItems(1))
Workbooks(FileNm.Name).Worksheets(txtClaimsShtName).Copy _
After:=Workbooks(txtMacroFname).Worksheets("By Market Report")
Workbooks(FileNm.Name).Worksheets(txtGeoClaimsShtName).Copy _
After:=Workbooks(txtMacroFname).Worksheets("By Market Report")
Workbooks(FileNm.Name).Close SaveChanges:=False
below:
If Err.Number <> 0 Then
MsgBox "Error"
End If
Set FileNm = Nothing
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
For the first time I now faced the error
Method ‘Copy’ of object ‘_Worksheet’ failed
I heard that this is a quite common error but I was not able to find a solution. When I searched trough SO, I found many posts about the
‘Range’ of object failed-Error.
Is this error caused by memory limitations of the workstation or is it a software related error?
Anyone got an idea to work around that problem?
In case it is of interest that is my VBA code:
Private Sub CommandButton1_Click()
MsgBox "Message"
Dim wb As Workbook
Set wb = Workbooks.Add(xlWBATWorksheet)
wb.Sheets(1).Name = "deletethissheet"
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Copy After:=wb.Sheets(wb.Sheets.Count)
Next
For Each ws In wb.Worksheets
ws.UsedRange.Formula = ws.UsedRange.Value
Dim sh As Shape
For Each sh In ws.Shapes
sh.Delete
Next
Next
For Each Link In wb.LinkSources(xlLinkTypeExcelLinks)
wb.BreakLink Name:=Link, Type:=xlLinkTypeExcelLinks
Next
wb.Sheets("deletethissheet").Delete
wb.SaveAs Replace(ThisWorkbook.FullName, ".xlsm", "_" & Format(Date, "yyyymmdd") & ".xlsx"), xlOpenXMLWorkbook
wb.Close SaveChanges:=False
End Sub
ws.Copy After:=wb.Sheets(wb.Sheets.Count)
Is the cause of the error.
|
|
Копирование некоторых листов в новую книгу с заменой типов я |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
||||||||
Ответить |
- Forum
- VBA Code & Other Help
- Excel Help
- Method ‘Copy’ of object’_Worksheet’ failed PROBLEM
-
09-18-2017, 03:55 AM
#1
Method ‘Copy’ of object’_Worksheet’ failed PROBLEM
Trying to create a new workbook but the Method ‘Copy’ of object’_Worksheet’ failed error keeps preventing the copy process.
I’ve attached a workbook. The Macro is run from the NewMODSheet WorkSheet.The Button says » Add A New WorkBook»
Caution! The attached worksheet crashes when the Macro runs.
But at the bottom of the sheet it shows copying.
A new Book1 is created, but it’s hidden from view, and the worksheet crashes.The same code works on other sheets.
-
09-18-2017, 04:42 AM
#2
It doesn’t crash for me, though of course you will get prompted about removing the VB project as you are trying to save a workbook with code in it as an xlsx file. Also, you really ought to specify the file format parameter.
-
09-18-2017, 03:30 PM
#3
Hi Aflatoon:
I have tried to save in every format I can think of, including
ActiveWorkbook.SaveAs "C:GraphicSheetTest.xls", FileFormat:=56
The line that causing the problem is the Copy portion of the code;
Sheets(Array("NewMODSheet", "Sheet2")).Copy
I’m still not sure what’s the problem.
-
09-19-2017, 02:13 AM
#4
Newer versions of Windows will not allow you to save a file to the Root directory. Try another folder.
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads ‘Solved’
-
09-20-2017, 11:16 PM
#5
mdmackillop:
I have already tried that.
ActiveWorkbook.SaveAs «C:DailyGraphicSheetTest.xls», FileFormat:=56Same exact error at the same location. Were you able to get it to save?
-
09-21-2017, 03:38 AM
#6
No issues with either of these:
ActiveWorkbook.SaveAs "C:VBAXGraphicSheetTest.xlsm", FileFormat:=52 ActiveWorkbook.SaveAs "C:VBAXGraphicSheetTest.xls", FileFormat:=56
As a last resort try
Worksheets(Array("NewMODSheet", "Sheet2")).Copy
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads ‘Solved’
-
11-14-2017, 05:41 PM
#7
Thanks mdmackillop;
I was away from Computers for a while. Traveling & A minor medical issue. Now, back to work.
Posting Permissions
- 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
- [VIDEO] code is On
- HTML code is Off
Forum Rules