我尝试下面的宏将excel名称“3”中的值复制到当前的excel名称“1”但是当我执行时我得到编译错误方法或数据成员未找到我不是很好的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.
答案 0 :(得分:2)
假设“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
答案 1 :(得分:0)
尝试此代码:假设您的代码是从当前工作簿运行的。您可以将当前工作簿称为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