gpt4 book ai didi

vb.net - 在 VBA 中复制工作表复制错误

转载 作者:行者123 更新时间:2023-12-02 11:21:35 25 4
gpt4 key购买 nike

您好,我在 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’的方法‘复制’失败”。它指向复制线。

最佳答案

我在从"template"文件复制工作表时遇到了类似的问题。我认为这是一个内存问题,在一定数量的复制和粘贴后会出现(取决于您的系统)。

根据您的工作表包含的内容,有一些解决方法。我不需要循环浏览许多工作簿,但我发现以下函数可以有效地执行相同的操作,没有任何问题。

不过,有几点需要注意,每次将工作表从一个工作簿复制到另一个工作簿时,创建两个新的 Excel 实例可能并没有帮助。为什么不能使用 Excel 实例,只需使用至少一个 Excel 实例。

Sub CopyWorksheet(wsSource As Worksheet, sName As String, wsLocation As Worksheet, sLocation As String)
'Instead of straight copying we just add a temp worksheet and copy the cells.
Dim wsTemp As Worksheet

'The sLocation could be a boolean for before/after. whatever.
If sLocation = "After" Then
Set wsTemp = wsLocation.Parent.Worksheets.Add(, wsLocation)
ElseIf sLocation = "Before" Then
Set wsTemp = wsLocation.Parent.Worksheets.Add(wsLocation)
End If

'After the new worksheet is created
With wsTemp
.Name = sName 'Name it
.Activate 'Bring it to foreground for pasting
wsSource.Cells.Copy 'Copy all the cells in the original
.Paste 'Paste all the cells
.Cells(1, 1).Select 'Select the first cell so the whole sheet isn't selected
End With
Application.CutCopyMode = False
End Sub

关于vb.net - 在 VBA 中复制工作表复制错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16991199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com