macros从一个工作簿复制并粘贴到当前工作簿

我尝试下面的macros将Excel中的名称“3”的值复制到当前的Excel名称“1”,但是当我执行我得到编译错误的方法或数据成员没有find我不擅长Vb脚本任何人请帮助我

Sub Update() Dim sPath As String Dim objExcel As Application Dim sValue As String Dim wbTarget As Workbooks Dim strName As String Set wbThis = ActiveWorkbook strName = ActiveSheet.Name sPath = "C:\Users\nikhil.surendran\Desktop\1" Set wbTarget = Workbooks.Open("C:\Users\nikhil.surendran\Desktop\3" & strName & ".xlsx") sValue = wbTarget.Sheets(1).Range("A1").Value Set objExcel = CreateObject("Excel.Application") With objExcel .Visible = False .DisplayAlerts = 0 .ActiveWorkbook.Sheets(1).Range("B11").Value = sValue .ActiveWorkbook.Save .Quit End With End Sub Thanks in Advance. 

试试这个代码:假设你的代码是从当前工作簿运行的。 您可以参考当前工作簿作为ThisWorkbook和您打开作为wbTarget的工作簿

 Sub Update() Dim sPath As String Dim sValue As String Dim wbTarget As Workbook Dim strName As String strName = ActiveSheet.Name ' Explicitly provide the sheet name sPath = "C:\Users\nikhil.surendran\Desktop\1" Set wbTarget = Workbooks.Open("C:\Users\nikhil.surendran\Desktop\3" & strName & ".xlsx") sValue = wbTarget.Sheets(1).Range("A1").Value ThisWorkbook.Sheets(1).Range("B11").Value = sValue ThisWorkbook.Save End Sub 

假定“3.xlsx”是您要从其中将数据复制到当前工作簿的文件名。并且还从第一张“3.xlsx”到当前工作簿的第一张。

  Sub Update() Dim wbkSource As Workbook Set wbkSource = Workbooks.Open("C:\Users\nikhil.surendran\Desktop\3.xlsx") wbkSource.Worksheets(1).Range("A1").CurrentRegion.Copy ThisWorkbook.Worksheets(1).Range("A1") End Sub