简体   繁体   English

VBA动态打开同一文件夹中的文件

[英]vba open file in same folder dynamically

Is it possible to have a number of worksheets in a folder called eg Tom Dick and Harry and a fourth called admin. 是否可能在名为Tom Dick和Harry的文件夹中有许多工作表,而名为admin的文件夹中有许多工作表。 Admin has a some data input cells one of which has a drop down menu to select Tom Dick or Harry. 管理员有一些数据输入单元格,其中一个具有下拉菜单以选择Tom Dick或Harry。 Then when the macro is triggered it does stuff on the relevant worksheet? 然后,当宏被触发时,它会在相关工作表上进行填充吗? I'm learning fast how to do the stuff from all of the other great QA's here, but I can't find anything specific enough. 我正在从其他所有出色的质量检查人员那里快速学习如何做这些事情,但是我找不到足够具体的东西。 Ive read that this may work: 我读过,这可能工作:

Workbooks.Open Filename:=ThisWorkbook.Path & "Tom.xls" Workbooks.Open文件名:= ThisWorkbook.Path和“ Tom.xls”

But how to make it more dynamic so that it could open Dick or Harry depending on the drop down selection? 但是如何使它更具动态性,使其可以根据下拉选择打开Dick或Harry?

Cheers in anticipation :-) 期待中的欢呼声:-)

Let me expand on my comment a bit here. 让我在这里稍微补充一下我的评论。 First, we will assume you have 4 workbooks in a folder: "Tom, "Dick", "Harry" and "admin". Let's also assume the worksheet in which you input data in the workbook "admin", is likewise called "admin". In the worksheet "admin", you have data validation in cell A1 that allows a user to choose one of three strings: "Tom", "Dick" and "Harry". I would do something like: 首先,我们假设您在一个文件夹中有4个工作簿:“ Tom”,“ Dick”,“ Harry”和“ admin”。还假设您在工作表“ admin”中输入数据的工作表也称为“ admin” “。在工作表“ admin”中,您在单元格A1中进行了数据验证,该数据验证使用户可以选择三个字符串之一:“ Tom”,“ Dick”和“ Harry”。我将执行以下操作:

Dim admin as Workbook, wb as Workbook
Dim str as String

Set admin = Thisworkbook 'assuming you are writing this code in the workbook admin, which seems to be the case
str = admin.Sheets("admin").Range("A1").Value2
Set wb = Workbooks.Open(admin.Path & "\" & str & ".xls") 'assuming the files are .xls, just change the extension if they are otherwise

Given this, you have a workbook variable wb that allows you to do things to the workbook after it's open. 有了这个,您就有了一个工作簿变量wb ,它允许您在工作簿打开后对其执行操作。

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

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