简体   繁体   English

Excel VBA选择原始工作簿

[英]Excel VBA Selecting Original Workbook

I am working on a Excel VBA script that requires me to copy and paste different details from the original excel workbook into 5 new excel workbooks in an orderly manner. 我正在使用Excel VBA脚本,该脚本要求我有序地将原始excel工作簿的不同详细信息复制并粘贴到5个新的excel工作簿中。 Also, I am adding the Macros into a Add-in so that it can be added into another computer. 另外,我正在将宏添加到加载项中,以便可以将其添加到另一台计算机中。 My only problem is that I am unable to reference the original workbook because: 我唯一的问题是,由于以下原因,我无法引用原始工作簿:

1) The original workbook's file name will always be changing, therefore I am unable to pull out the workbook based on its name/assign the workbook name to a variable. 1)原始工作簿的文件名将始终更改,因此我无法根据其名称提取工作簿/将工作簿名称分配给变量。

2) Excel VBA have ActiveWorkbook/ThisWorkbook property but it does not ActiveWorkbook Property will always be changing, depending on the current workbook that is active. 2)Excel VBA具有ActiveWorkbook / ThisWorkbook属性,但并非ActiveWorkbook属性会一直更改,具体取决于当前处于活动状态的工作簿。 That being said, ActiveWorkbook will not work for me as the creation of new workbooks will overwrite the original workbook. 就是说,ActiveWorkbook对我不起作用,因为新工作簿的创建将覆盖原始工作簿。 ThisWorkbook property does not work for me because the VBA codes will be stored in an Add-in, but I want to save the new files in the original workbook's location instead (This is when I use ThisWorkbook.Path to identify the location). ThisWorkbook属性对我不起作用,因为VBA代码将存储在外接程序中,但是我想将新文件保存在原始工作簿的位置(这是在我使用ThisWorkbook.Path标识位置时)。

Does anyone know if I can make Excel VBA read the original file's location and lock that value into a variable such that I can always call it back again when I need to reference it again? 有谁知道我是否可以让Excel VBA读取原始文件的位置并将该值锁定为变量,以便在需要再次引用它时始终可以再次调用它?

Below is a sample code that I have. 下面是我的示例代码。 Any help will be greatly appreciated. 任何帮助将不胜感激。 Thank you! 谢谢!

Option Explicit

Sub new()

    Dim createWb As Workbook
    Dim Originalwks As Worksheet
    Dim createWbName As String

    Set Originalwks = ActiveWorkbook.ActiveSheet

    createWbName = Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4)
    Set createWb = Workbooks.Add
    createWb.SaveAs Filename:=ThisWorkbook.Path & Application.PathSeparator & createWbName
    createWb.Sheets("Sheet1").Name = createWbName

    Originalwks.UsedRange.Copy
    createWb.Worksheets(createWbName).Range("A1").PasteSpecial xlValues

    'I will still be adding new workbooks by copying values from the original workbook. 
    'But how do I pull out the original workbook again? 

    createWb.Close SaveChanges:=True

End Sub

You are confused about how to use ActiveWorkbook and ThisWorkbook objects. 您对如何使用ActiveWorkbookThisWorkbook对象感到困惑。 The best approach is always to take the control of all of the elements you are dealing with in your process, in this case you have a workbook that is your original workbook and you want to use its data and path later in your routine. 最好的方法始终是控制流程中要处理的所有元素,在这种情况下,您有一个工作簿,它就是原始工作簿,并且您想在以后的例程中使用它的数据和路径。 It does not matter if you run your routine from an add-in or from the original workbook itself, or even from another workbook. 从外接程序,原始工作簿本身,甚至从另一个工作簿运行例程都没关系。 Your original workbook has a path that you can define it in your code either dynamically or just hard coded. 原始工作簿具有一个路径,您可以在代码中动态定义它,也可以仅对其进行硬编码。 So here are some options you can use: 因此,您可以使用以下一些选项:

If the original workbook is open and active then do this to always refer to it and retrieve its path: 如果原始工作簿处于打开状态且处于活动状态,请执行以下操作以始终引用它并检索其路径:

Dim WB_Orig As Workbook
Set WB_Orig = ActiveWorkbook

if activeworkbook changes it will not cause a problem. 如果activeworkbook发生更改,则不会造成问题。 Also, regardless of running the code from add-in or the original workbook itself, activewokbook will be that one (the one you see on your screen), if you run it from add-in ThisWorkbook will refer to the add-in workbook and NOT your original workbook. 另外,无论从加载项运行代码还是从原始工作簿本身运行代码, activewokbook都将是该代码(您在屏幕上看到的代码),如果从加载项运行它,则ThisWorkbook将引用加载项工作簿,并且不是您的原始工作簿。

If you run the code from the add-in, I would try to locate the original workbook and open it like this and set it to an object at the same time: 如果您从加载项中运行代码,我将尝试找到原始工作簿并像这样打开它并将其同时设置为一个对象:

Set WB_Orig = Application.Workbooks.Open("filename") 'filename is the path to your original workbook

also, there could be a case that the workbook is open so it is better to check all the open workbook names before trying to open it. 同样,可能存在工作簿处于打开状态的情况,因此最好在尝试打开它之前先检查所有打开的工作簿名称。 You can iterate over the open workbooks and check their names and if you had a match simply assign it as the WB_Orig : 您可以遍历打开的工作簿并检查其名称,如果有匹配项,只需将其分配为WB_Orig

Function GetOriginalWorkbook(sFilename As String)
    Dim WB As Workbook
    For Each WB In Application.Workbooks
        If WB.Name = sFilename Then
            Set GetOriginalWorkbook = WB
            Exit For
        End If
    Next
End Function

using the above function in your sub you can easily do: 使用您的子程序中的上述功能,您可以轻松地执行以下操作:

Set WB_Orig = GetOriginalWorkbook("Book1") 'you need to change Book1 with the name of your origianl workbook.

If Original workbook is open then you have it as an object already assigned to WB_Orig otherwise it will be Nothing and you should open it: 如果原始工作簿已打开,则将其作为已分配给WB_Orig的对象,否则将为Nothing ,应打开它:

Set WB_Orig = GetOriginalWorkbook("Book1")

if WB_Orig is Nothing then
     Set WB_Orig = Application.Workbooks.Open("filename")
end if

Another trick would be setting on error to resume next before trying to open the original workbook and thensetting it back to goto 0 . 另一个技巧是在尝试打开原始工作簿然后将其设置回goto 0之前,将on error设置为resume next This way if the original workbook was not open, Excel will open it, if it was open, an error should be thrown, but since you told excel to ignore it, it will resume executing the code and you should be fine. 这样,如果未打开原始工作簿,则Excel将打开它,如果已打开,则将引发错误,但是由于您告诉excel忽略它,它将恢复执行代码,您应该可以。 Using on error resume next is tricky and as a good practice you should always try to foresee and control the errors not ignore them. on error resume next使用on error resume nexton error resume next棘手的问题,作为一种好习惯,您应该始终预见和控制错误,不要忽略它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM