[英]Copying of a cell value from one workbook to another
球隊,
是Excel Macro的新手,它試圖將單元格E15的值從9月的工作簿名稱復制到另一種工作簿名稱格式的D15中,並且無法使用下面的代碼來實現。 請幫我
我使用下面的代碼來做到這一點。
Sub COPYCELL()
Dim wbk As Workbook
strFirstFile = "c:\documents and Settings\msivas\Desktop\James\September.xls"
strSecondFile = "c:\documents and Settings\msivas\Desktop\James\Format.xls"
Set wbk = Workbooks.Open(strFirstFile)
With wbk.Sheets("Summary")
Range("E15").Copy
End With
Set wbk = Workbooks.Open(strSecondFile)
With wbk.Sheets("sheet1")
Range("D12").PasteSpecial Paste:=xlPasteAll
End With
End Sub
正在接收錯誤,無法得知。
試試這個更新。 通過不回收wbk變量,可以避免問題。
Sub COPYCELL()
Dim wbkOrg As Workbook, wbkDest as workbook
dim strFirstFile as string, strSecondFile as string
strFirstFile = "c:\documents and Settings\msivas\Desktop\James\September.xls"
strSecondFile = "c:\documents and Settings\msivas\Desktop\James\Format.xls"
Set wbkOrg = Workbooks.Open(strFirstFile)
Set wbkDest = Workbooks.Open(strSecondFile)
wbkOrg.Sheets("Summary").Range("E15").Copy wbkDest.Sheets("sheet1").Range("D12")
End Sub
根據我的評論,這對我有用:
Option Explicit
Sub COPYCELL()
Dim wbk1 As Workbook, wbk2 As Workbook
Dim strFirstfile As String, strSecondFile As String
strFirstfile = "c:\documents and Settings\msivas\Desktop\James\September.xls"
strSecondFile = "c:\documents and Settings\msivas\Desktop\James\Format.xls"
Set wbk1 = Workbooks.Open(strFirstfile)
Set wbk2 = Workbooks.Open(strSecondFile)
With wbk1.Sheets("Summary")
.Range("E15").Copy
End With
With wbk2.Sheets("sheet1")
.Range("D12").PasteSpecial Paste:=xlPasteAll
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.