【发布时间】:2013-06-07 19:03:25
【问题描述】:
您好,我在 VB 中将工作表从一个工作簿复制到另一个工作簿时遇到问题。我的代码在全新的工作簿上运行良好,但过了一段时间它会中断并给我这个错误:“对象'_Worksheet'的方法'复制'失败。很多人建议保存工作簿并在复制时重新打开它。我试过了,它仍然没有用。我还检查了名称是否变得很长。我在复制之前将工作表的名称设置为计数器,但我仍然遇到错误。我真的很困惑,并且希望有人可能已经找到解决方案。而且两个工作簿中只有 3 个工作表。
'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 次)。即使我删除了dest_wb中的工作表,最终也会出现此错误“对象'_Worksheet'的方法'复制'失败”。它指向复制行。
【问题讨论】:
-
你真的在初始化
dest_wb吗? -
是的,它总是初始化的。
-
请粘贴所有相关代码。当我初始化两个工作簿时,您的代码对我有用。床单是否以某种方式受到保护?另外,您对何时不起作用的描述并没有真正的帮助-
after awhile it breaks and gives me this error您是什么意思? -
我所说的“这个工作簿会在一段时间后中断”的意思是我可以在上面多次运行这段代码(可能大约 30 次)。即使我删除了dest_wb中的工作表,最终也会出现此错误“对象'_Worksheet'的方法'复制'失败”。它指向复制行。