[英]Excel synchronize workbooks with vba
I'd like to ask if you could help me with copying some worksheet data from workbook A into my active workbook (workbook B) 我想问一下您是否可以帮助我将工作簿A中的一些工作表数据复制到我的活动工作簿(工作簿B)中
I have the following code in the main workbook to copy the data from workbook A 我在主工作簿中具有以下代码,用于从工作簿A复制数据
Public Sub Worksheet_Activate()
test
End Sub
Sub test()
Dim Wb1 As Workbook
Dim MainBook As Workbook
'Open All workbooks first:
Set Wb1 = Workbooks.Open("Z:\Folder\WorkbookA.xlsm")
'Set MainBook = Workbooks.Open(Application.ActiveWorkbook.FullName)
Set MainBook = Application.ActiveWorkbook
'Now, copy what you want from wb1:
Wb1.Sheets("Projekte").Cells.Copy
'Now, paste to Main worksheet:
MainBook.Worksheets("Projekte").Range("A1").PasteSpecial xlPasteAll
Application.CutCopyMode = False
'Close Wb's:
Wb1.Close SaveChanges:=False
End Sub
I know, that it opens worksheet A and that it highlights and copys the data. 我知道,它打开了工作表A,并且突出显示并复制了数据。
The script wont paste it into worksheet B (where the script is executed from) 该脚本不会将其粘贴到工作表B(从中执行脚本)
Anybody know what i did wrong? 有人知道我做错了吗?
Kindest regards, and thanks for any help ! 亲切的问候,谢谢您的帮助!
You should set the Mainbook (destination) first before the origin (if you're going to use ActiveWorkbook
). 您应该首先在原点之前设置Mainbook(目的地)(如果要使用
ActiveWorkbook
)。
'Set MainBook First
Set MainBook = ThisWorkbook
'Open All workbook:
Set Wb1 = Workbooks.Open("Z:\Folder\WorkbookA.xlsm")
Just for clarity, it's just me being OC on this one. 为了清楚起见,这只是我担任OC的职责。
you have to properly reference your workbook and worksheet objects 您必须正确引用您的工作簿和工作表对象
if you have to paste the whole content of range (including formatting, comments, ...), then you want to code like follows (explanations in comments): 如果您必须粘贴范围的全部内容(包括格式,注释等),则需要编写如下代码(注释中的解释):
Option Explicit
Sub test()
Dim targetSheet As Worksheet
Set targetSheet = ActiveSheet 'store currently active sheet
Workbooks.Open("Z:\Folder\WorkbookA.xlsm").Sheets("Projekte").UsedRange.Copy Destination:=targetSheet.Range("A1") ' open the wanted workbook and copy its "Projekte" sheet used range to 'targetSheet (i.e.: the sheet in the workbook where it all started) from its cell A1
ActiveWorkbook.Close SaveChanges:=False ' close the currently active workbook (i.e. the just opened one)
End Sub
if you only need to paste values, then this is the way to go (much faster!): 如果您只需要粘贴值,那么这就是方法(快得多!):
Option Explicit
Sub test()
Dim targetSheet As Worksheet
Set targetSheet = ActiveSheet 'store currently active sheet
With Workbooks.Open("Z:\Folder\WorkbookA.xlsm").Sheets("Projekte").UsedRange ' open the wanted workbook and reference its sheet "Projekte" used range
targetSheet.Range("A1").Resize(.Rows.Count, .Columns.Count).Value = .Value
.Parent.Parent.Close SaveChanges:=False 'close the parent workbook of referenced range (i.e., the newly opened workbook)
End With
End Sub
My recommendation is never to use ActiveWorkbook
. 我的建议是永远不要使用
ActiveWorkbook
。
In most cases when people use ActiveWorkbook
they actually meant to use ThisWorkbook
. 在大多数情况下,当人们使用
ActiveWorkbook
他们实际上是在使用ThisWorkbook
。 The difference is: 区别在于:
ActiveWorkbook
is the currently selected one which is "on top of the screen" in exact that moment when your code runs. ActiveWorkbook
是当前选中的代码,它恰好在您的代码运行的那一刻“在屏幕上方”。 That can be any workbook the user just clicked on while your code runs. 可以是用户在代码运行时单击的任何工作簿。 So you never can be 100% sure to get the right workbook.
因此,您永远无法百分百确定获得正确的工作簿。
ThisWorkbook
is the actual workbook your code runs at the moment. ThisWorkbook
是您的代码当前运行的实际工作簿。 And this doesn't change ever. 而且这永远不会改变。 So this is a well defined reference and no gamble about which workbook is on top at the moment.
因此,这是一个定义明确的参考,目前尚无关于哪个工作簿最重要的内容。
About why your approach did not work 关于为什么您的方法无效的原因
Set Wb1 = Workbooks.Open("Z:\Folder\WorkbookA.xlsm") 'this line makes WorkbookA the active one
Set MainBook = Application.ActiveWorkbook 'this makes MainBook = WorkbookA
Therefore a simple Set MainBook = Application.ThisWorkbook
should work here. 因此,一个简单的
Set MainBook = Application.ThisWorkbook
应该在这里工作。
Another recommendation 另一个建议
Sheets
and Worksheets
is not the same. Worksheets
Sheets
和Worksheets
是不同的。 Make sure you never use Sheets
when you can use Worksheets
. 确保可以使用
Worksheets
Sheets
时不要使用Worksheets
。
Sheets
contains worksheets and charts Sheets
包含工作表和图表 Worksheets
contain only worksheets Worksheets
仅包含工作表 An example Sheets(1).Range("A1")
fails if it is a chart. 如果示例
Sheets(1).Range("A1")
是图表,则会失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.