我可以使用什么.vbs代码将整个工作表从excel文件复制到另一个?
我已尝试使用以下内容,但第2行和第3行会弹出错误(Error Image),如果没有它们,脚本会运行但不执行任何操作。
Sub Copy_Sheet()
Dim x As Workbook
Dim y As Workbook
Set x = Workbooks.Open("C:\File1.xls")
Set y = Workbooks.Open("C:\File2.xlsx")
y.Sheets("Sheet1").Range("A1").Value = x.Sheets("Sheet1").Range("A1")
y.Save
y.close
x.Close
End Sub
答案 0 :(得分:1)
Dim xlapp ' as excel object
Dim WSx, WSy ' as excel worksheet
Dim x, y ' as workbook
Set xlapp = CreateObject("Excel.Application")
Set x = xlapp.Workbooks.Open("C:\File1.xls")
Set y = xlapp.Workbooks.Open("C:\File2.xlsx")
Set WSx = x.Worksheets("Sheet1")
Set WSy = y.Worksheets("Sheet1")
WSx.Copy WSy ' copy worksheet to other workbook
Set WSx = nothing
Set WSy = nothing
y.Save
y.close
x.Close
这应该将工作表从x复制到y而没有任何问题。
答案 1 :(得分:0)
你可以试试这个
Dim xlapp ' as excel object
Dim WSx, WSy ' as excel worksheet
Dim x, y ' as workbook
Set xlapp = CreateObject("Excel.Application")
Set x = xlapp.Workbooks.Open("C:\File1.xls")
Set y = xlapp.Workbooks.Open("C:\File2.xlsx")
Set WSx = x.Worksheets("Sheet1")
Set WSy = y.Worksheets("Sheet1")
WSy.Range("A1").value = WSx.Range("A1").value
Set WSx = nothing
Set WSy = nothing
y.Save
y.close
x.Close
答案 2 :(得分:0)