[英]Copy sheet from excel workbook to another workbook
Below is my code for the copying process from one workbook to another. 下面是我从一个工作簿复制到另一工作簿的代码。 I looked up a lot of similar issues but i could not get this working.
我查了很多类似的问题,但无法正常工作。 when I run this the two files open up and then i get a third one called book1 with all results.
当我运行此文件时,将打开两个文件,然后我得到一个名为book1的第三个文件,其中包含所有结果。 then i get an error "Copy method of Worksheet class failed".
然后我得到一个错误“工作表类的复制方法失败”。 What Im trying to do is copy the general report sheet from o.Book to xBook.
我想做的是将一般报表从o.Book复制到xBook。 I want to leave the books open for now until this is correct but i will use Xbook later.
我想暂时将这些书保持打开状态,直到这是正确的为止,但是稍后我将使用Xbook。 Can I get help with this please?
我可以帮忙吗?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.Click
Dim oExcel As Excel.ApplicationClass
Dim oBook As Excel.WorkbookClass
Dim oBooks As Excel.Workbooks
Dim xExcel As Excel.ApplicationClass
Dim xBook As Excel.WorkbookClass
Dim xBooks As Excel.Workbooks
Dim user As String
Dim opath As String
Dim opathS As String
Dim timeStamp As DateTime = DateTime.Now
Dim path2 As String
Label1.Text = "Working..."
'Get the current system user user and set path to file
user = Environment.UserName
opath = "C:\Users\" + user + "\Downloads\ADC Open.xls"
path2 = "C:\Users\" + user + "\Downloads\Personal.xlsm"
opathS = "C:\Users\" + user + "\Desktop\Report.xls"
'Create first object
oExcel = CreateObject("Excel.Application")
oExcel.DisplayAlerts = False
oExcel.Visible = True
oBooks = oExcel.Workbooks
'Create second object
xExcel = CreateObject("Excel.Application")
xExcel.DisplayAlerts = False
xExcel.Visible = True
xBooks = xExcel.Workbooks
'open first book
oBook = oBooks.Open(opath)
'open second book
xBook = xBooks.Open(path2)
oBook.Worksheets("general_report").Copy(After:=xBook.Worksheets("general_report"))
'Run the subroutine.
'xExcel.Run("Execute")
'xExcel.DisplayAlerts = False
'Delete sheet not needed any more
'xBook.Sheets("general_report").Delete
'xExcel.DisplayAlerts = False
'Save results to new file
xBook.SaveAs(opathS)
Label1.Text = "File saved at: " + opathS
'Close the workbook and quit Excel.
oBook.Close(False)
System.Runtime.InteropServices.Marshal.
ReleaseComObject(oBook)
oBook = Nothing
System.Runtime.InteropServices.Marshal.
ReleaseComObject(oBooks)
oBooks = Nothing
oExcel.Quit()
System.Runtime.InteropServices.Marshal.
ReleaseComObject(oExcel)
oExcel = Nothing
'Delete original file after finished with it
'System.IO.File.Delete(opath)
End Sub
Can't add a comment yet, but if VB is the same across all platforms, shouldn't you Set
the variable after declaring it ? 还不能添加注释,但是如果在所有平台上VB都相同,那么是否应该在声明变量后
Set
变量?
Set MyObject = YourObject ' Assign object reference.
Set MyObject = Nothing ' Discontinue association.
After all the responses I started looking into these object settings and find code that help with the explainations, I refactored my previous version and this is what I came up with. 在所有答复之后,我开始研究这些对象设置并找到有助于解释的代码,然后重构了以前的版本,这就是我想出的。 It works like a charm now.
现在它就像一种魅力。 Thanks everyone for the help and comments.
感谢大家的帮助和评论。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 私有子Button1_Click(作为对象发送,作为EventArgs发送)处理Button1.Click
Dim xlApp As Excel.Application = New Excel.Application
Dim user As String
Dim sourcePath As String
Dim targetPath As String
Dim savePath As String
Label1.Text = "Working..."
user = Environment.UserName
sourcePath = "C:\Users\" + user + "\Desktop\Report\ADC Open (Dell GTIE JIRA).xls"
targetPath = "C:\Users\" + user + "\Desktop\Report\Personal1.xlsm"
savePath = "C:\Users\" + user + "\Desktop\Report\Report" & Format(Now(), "DD-MMM-YYYY") & ".xlsm"
Dim wbSourceBook As Excel.Workbook = xlApp.Workbooks.Open _
(sourcePath, ReadOnly:=False)
Dim wbTargetBook As Excel.Workbook = xlApp.Workbooks.Open _
(targetPath, ReadOnly:=False)
'Excel expects to receive an array of objects that
'represent the worksheets to be copied or moved.
Dim oSheetsList() As Object = {"general_report"}
wbSourceBook.Sheets(oSheetsList).Copy(Before:=wbTargetBook.Worksheets(1))
wbSourceBook.Close(True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.