I have created a vba script to copy all data from multiple pdf files and paste in a excel in different tabs with the same name of the pdf files.
我创建了一个vba脚本来复制来自多个pdf文件的所有数据,并使用相同名称的pdf文件粘贴到不同选项卡中的Excel中。
Problem is its copying data from pdf files and pasting in the same tab of the worksheet replacing one over another. Please let me where i am going wrong with this.
问题是它从pdf文件中复制数据并粘贴在工作表的相同选项卡中,将其替换为另一个。请告诉我这里我出错了。
1 个解决方案
#1
5
You need to reset wsOutp
before checking if it is nothing. As written, you are setting wsOutp
to reference a sheet on your initial loop. You then must reset it to nothing before the next loop, because when you try to set it to reference another sheet which may not exist, if that fails the reference still points to the original sheet. So what ends up happening, is you delete the sheet on the next step.
您需要重置wsOutp,然后再检查它是否为空。如上所述,您将wsOutp设置为在初始循环中引用工作表。然后,您必须在下一个循环之前将其重置为空,因为当您尝试将其设置为引用可能不存在的另一个工作表时,如果失败,则引用仍指向原始工作表。那么最终会发生什么,是你在下一步删除工作表。
So in reality your code is not overwriting to the same sheet, but you are deleting each sheet you create, so in the end you are left with only one sheet.
所以实际上你的代码不会覆盖到同一张纸上,但是你要删除你创建的每张纸,所以最后你只剩下一张纸。
Add the line below:
添加以下行:
' Delete sheet with filename if exists
Set wsOutp = Nothing
On Error Resume Next
Set wsOutp = Sheets(strFile)
On Error GoTo 0
If Not wsOutp Is Nothing Then
wsOutp.Delete
End If
#1
5
You need to reset wsOutp
before checking if it is nothing. As written, you are setting wsOutp
to reference a sheet on your initial loop. You then must reset it to nothing before the next loop, because when you try to set it to reference another sheet which may not exist, if that fails the reference still points to the original sheet. So what ends up happening, is you delete the sheet on the next step.
您需要重置wsOutp,然后再检查它是否为空。如上所述,您将wsOutp设置为在初始循环中引用工作表。然后,您必须在下一个循环之前将其重置为空,因为当您尝试将其设置为引用可能不存在的另一个工作表时,如果失败,则引用仍指向原始工作表。那么最终会发生什么,是你在下一步删除工作表。
So in reality your code is not overwriting to the same sheet, but you are deleting each sheet you create, so in the end you are left with only one sheet.
所以实际上你的代码不会覆盖到同一张纸上,但是你要删除你创建的每张纸,所以最后你只剩下一张纸。
Add the line below:
添加以下行:
' Delete sheet with filename if exists
Set wsOutp = Nothing
On Error Resume Next
Set wsOutp = Sheets(strFile)
On Error GoTo 0
If Not wsOutp Is Nothing Then
wsOutp.Delete
End If