excel vba:将单元格值从一个工作簿复制到另一个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27357586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:26:23  来源:igfitidea点击:

excel vba: copy cell value from one workbook to another?

excelvbaexcel-vbacopy

提问by kyle ridge

I am trying to copy the value from cell b from the active row on sheet 'SupplierTracking' when the text 'send survey' is clicked in column BAand paste this in cell A2on my other workbook 'Supplier Survey' on the 'Data' sheet.

SupplierTracking当在列中单击文本“发送调查”时,我试图从工作表“ ”上的活动行中复制单元格 b 中的值,BA并将其粘贴到“ ”工作表A2上我的其他工作簿“ Supplier Survey”的单元格中Data

For some reason I am not getting any error, however nothing is being pasted into the cell A2on my other workbook.

出于某种原因,我没有收到任何错误,但是没有将任何内容粘贴到A2我的其他工作簿上的单元格中。

could someone please show me where I am going wrong. thanks in advance

有人可以告诉我我哪里出错了。提前致谢

If Target.Column = Range("BA1").Column And Range("BA" & ActiveCell.Row).Value = "Send Survey" Then
Application.ScreenUpdating = False
Dim wb As Workbook
Dim ws1112 As Worksheet
Dim ws2221 As Worksheet
Set ws1112 = Sheets("SupplierTracking")
Set wb = Workbooks.Open("\{server address}\assets\Supplier Survey.xls")
Set ws2221 = wb.Sheets("Data")
ws2221.Range("A2").Value = ws1112.Range("B" & ActiveCell.Row).Value
'Optional if you want to close the workbook afterwards
wb.Close SaveChanges:=True
Application.ScreenUpdating = True
End If

回答by kyle ridge

I managed to resolve the answer to my question, I needed to save activeCell.Row as a string variable like so

我设法解决了我的问题的答案,我需要像这样将 activeCell.Row 保存为字符串变量

If Target.Column = Range("BA1").Column And Range("BA" & ActiveCell.Row).Value = "Send Survey" Then

Dim wb As Workbook
Dim ws1112 As Worksheet
Dim ws2221 As Worksheet
Dim s As String
Dim r As String
Set ws1112 = Sheets("SupplierTracking")
s = ws1112.Range("B" & ActiveCell.Row).Value
r = ws1112.Range("C" & ActiveCell.Row).Value
Set wb = Workbooks.Open("\UKSH000-File06\Purchasing\New_Supplier_Set_Ups_&_Audits\assets\Supplier Survey.xls")
Set ws2221 = wb.Sheets("Data")

ws2221.Range("A2").Value = s
ws2221.Range("B2").Value = r
'Optional if you want to close the workbook afterwards
wb.Close SaveChanges:=True

End If