[英]Toggling between 3 workbooks
I got a MAIN WORKBOOK which creates a new workbook with specific worksheets and headers and it will not save the workbook so it will be a random name like Book1 or Book2. 我有一个MAIN WORKBOOK,它使用特定的工作表和标题创建一个新的工作簿,并且不会保存该工作簿,因此它是一个随机名称,如Book1或Book2。 This part is successful. 这部分是成功的。 Next thing that it will do is open a file and copy the data of the opened file to the created UNSAVED workbook ("Book1" or whatever is the name) 接下来要做的是打开一个文件,然后将打开的文件的数据复制到创建的UNSAVED工作簿(“ Book1”或任何名称)中。
Problem is I can't switch windows between the source data workbook which is the opened file and the unsaved workbook. 问题是我无法在已打开文件的源数据工作簿和未保存的工作簿之间切换窗口。 I tried windows("workbook name").activate but it only works for the specific name. 我试过windows(“ workbook name”)。activate,但仅适用于特定名称。 I need the unsaved workbook to be activated. 我需要激活未保存的工作簿。 In short I have 3 opened workbooks. 简而言之,我有3个已打开的工作簿。
Sub CopyData()
Dim Newbook As Window
Dim wb As Workbook, wb2 As Workbook
Dim ws As Worksheet
Dim vFile As Variant
'Set source workbook
Set wb = ActiveWorkbook
'Open the target workbook
vFile = Application.GetOpenFilename("All-Files,*.xl**", _
1, "Select One File To Open", , False)
'if the user didn't select a file, exit sub
If TypeName(vFile) = "Boolean" Then Exit Sub
Workbooks.Open vFile
Set wb2 = ActiveWorkbook
Worksheets("Sheet1").Range("A3:A65536").Copy
Windows("Book1").Activate 'Part where it should open the "unsaved file" and copy it
wb.Worksheets("Prices").Range("A3").PasteSpecial (xlPasteValues) ' unsaved file
End Sub
You can get the reference to the newly opened workbook by assigning the result of the function Workbooks.Open
to a variable. 通过将功能Workbooks.Open
的结果分配给变量,可以获取对新打开的工作簿的引用。 So instead of those two rows: 因此,而不是这两行:
Workbooks.Open vFile
Set wb2 = ActiveWorkbook
you need to put this one: 您需要输入以下内容:
Set wb2 = Workbooks.Open(vFile)
Now you have two variables: 现在您有了两个变量:
wb
pointing to your original workbook, wb
指向您的原始工作簿, wb2
pointing to the newly created workbook. wb2
指向新创建的工作簿。 When referencing to ranges you need to put the proper variable at the beginning, ie: 当引用范围时,您需要在开头放置适当的变量,即:
wb.Worksheets("Sheet1").Range("A3:A65536").Copy
This line Windows("Book1").Activate
is no needed anymore, since if you refer to workbooks by variables you don't need to activate them to copy & paste data. 此行Windows("Book1").Activate
不再需要,因为如果您通过变量引用工作簿,则无需激活它们即可复制和粘贴数据。
You were almost there, you just need to use the Workbooks
instead of Windows
. 您快要准备好了,只需要使用Workbooks
而不是Windows
。
Workbooks("workbook name").Activate
See MSDN for documentation on the Workbook
object . 有关Workbook
对象的文档,请参见MSDN。
EDIT: Originally misunderstood your question. 编辑:最初误解了您的问题。 @Mielk has now answered how you can achieve what you're after. @Mielk现在已经回答了您如何实现自己的目标。 But, for clarity, see the answer below too. 但是,为清楚起见,也请参见下面的答案。
Not sure if you know that the unsaved workbook will consistently be called "Book*", but if it is, you can use this: 不知道您是否知道未保存的工作簿将始终称为“ Book *”,但是如果是这样,则可以使用以下方法:
Sub workbookActivate()
Dim wb3 As Workbook, wkb As Workbook
For Each wkb In Workbooks
If Left(wkb.Name, 4) = "Book" Then
Set wb3 = wkb
Exit For
End If
Next wkb
wkb.Activate
End Sub
Just an alternative. 只是一种选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.