안녕하세요. VB에서 한 통합 문서에서 다른 통합 문서로 워크 시트를 복사하는 데 문제가 있습니다. 내가 가지고있는 코드는 새로운 통합 문서에서 잘 작동하지만 잠시 후 깨져서이 오류가 발생합니다. " '_Worksheet'개체의 '복사'메서드가 실패했습니다. 많은 사람들이 복사 할 때 통합 문서를 저장하고 다시 열 것을 제안했습니다. 나는 그것을 시도하고 여전히 작동하지 않았다. 나는 또한 이름이 정말로 길어질 지 여부를 확인했다. 복사하기 전에 워크 시트의 이름을 카운터에 설정했는데 여전히 버그가있다. 정말 혼란 스럽다. . 누군가가이 문제를 해결하기위한 방안을 알아 낸 수 있습니다 희망도 모두 통합 문서는 그들에 3 개 워크 시트를VBA에서 복사 워크 시트 복사 오류
'Copies all the worksheets from one workbook to another workbook
'source_name is the Workbook's FullName
'dest_name is the Workbook's FullName
Function copyWorkbookToWorkbook(source_name As String, dest_name As String) As Boolean
Dim dest_wb As Workbook
Dim source_wb As Workbook
Dim dest_app As New Excel.Application
Dim source_app As New Excel.Application
Dim source_ws As Worksheets
Dim counter As Integer
Dim num_ws As Integer
Dim new_source As Boolean
Dim new_dest As Boolean
Dim ws As Worksheet
Dim regex As String
Application.ScreenUpdating = False
If source_name = "" Or dest_name = "" Then
MsgBox "Source and Target must both be selected!", vbCritical
copyWorkbookToWorkbook = False
ElseIf GetAttr(dest_name) = vbReadOnly Then
MsgBox "The target file is readonly and cannot be modified", vbCritical
copyWorkbookToWorkbook = False
Else
regex = "[^\\]*\.[^\\]*$" 'Gets only the filename
copyWorkbookToWorkbook = True
If (isWorkbookOpen(source_name)) Then
Set source_wb = Workbooks(regExp(source_name, regex, False, True)(0).Value)
Else
Set source_wb = source_app.Workbooks.Open(source_name)
new_source = True
End If
If (isWorkbookOpen(dest_name)) Then
Set dest_wb = Workbooks(regExp(dest_name, regex, False, True)(0).Value)
Else
Set dest_wb = dest_app.Workbooks.Open(dest_name)
new_dest = True
End If
'Clean the workbooks before copying the data
'Call cleanWorkbook(source_wb)
'Call cleanWorkbook(dest_wb)
'Copy each worksheet from source to target
counter = 0
source_wb.Activate
For Each ws In source_wb.Worksheets
MsgBox dest_wb.Worksheets.Count
ws.Copy After:=dest_wb.Worksheets(dest_wb.Worksheets.Count)
counter = counter + 1
Next ws
'Save and close any newly opened files
If (new_dest) Then
dest_wb.Application.DisplayAlerts = False
dest_wb.SaveAs Filename:=dest_name, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
dest_wb.Application.CutCopyMode = False
dest_wb.Close
End If
If (new_source) Then
source_wb.Application.DisplayAlerts = False
source_wb.SaveAs Filename:=source_name, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
source_wb.Close
End If
MsgBox counter & " worksheets have been cleaned and copied.", vbInformation + vbOKOnly
End If
'Cleanup
Set dest_wb = Nothing
Set source_wb = Nothing
Set dest_app = Nothing
Set source_app = Nothing
Set source_ws = Nothing
Set ws = Nothing
End Function
Function regExp(str As String, pattern As String, ignore_case As Boolean, glo As Boolean) As MatchCollection
Dim regex As New VBScript_RegExp_55.regExp
Dim matches As MatchCollection
regex.pattern = pattern
regex.IgnoreCase = ignore_case
regex.Global = glo
Set regExp = regex.Execute(str)
End Function
편집 :. 내가 "휴식 잠시 후이 통합 문서"를 의미하는 것은 내가 거기에이 코드를 실행할 수 있다는 것입니다 여러 번 (어쩌면 약 30 번). 결국이 오류가 나타납니다 "메서드 ''복사 ''_Worksheet '개체의"삭제 된 경우에도 dest_wb의 워크 시트. 복사 행을 가리 킵니다.
실제로'dest_wb'를 초기화 하시겠습니까? – enderland
예, 항상 초기화되어 있습니다. – Taztingo
관련 코드를 모두 붙여 넣으십시오. 두 통합 문서를 초기화 할 때 코드가 작동합니다. 시트가 어떻게 든 보호됩니까? 또한 작동하지 않는 경우에 대한 설명이 도움이되지 않습니다. '잠시 후에 깨져서이 오류가 발생합니다.'무슨 뜻입니까? – enderland