0

Team,

Am new to excel Macro and trying to copy a value of a Cell E15 from a workbook name september to D15 of another workbook name format and am unable to do that with the below coding. Please help me

I used the below coding to do so.

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

Am receiving an error and unable to know.

4

2 回答 2

3

试试这个更新。通过不回收 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
于 2012-09-26T18:48:14.067 回答
0

根据我的评论,这对我有用:

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
于 2012-09-26T18:58:06.983 回答