用参数打开工作簿 [英] Open workbook with arguments

查看:66
本文介绍了用参数打开工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:是否可以打开工作簿并通过此操作传递参数/参数?

Question : Is there a way to open a workbook and pass an argument/parameter through this action ?

想法是为了区分用户直接打开工作簿B的情况(这将触发工作簿B的自动操作 _ 打开代码),以及从其他工作簿的代码中打开工作簿B的情况

The idea is to differentiate the case where a user directly open Workbook B (which will trigger its Auto_Open code) and the case where Workbook B is opened from the code of an other Workbook.

推荐答案

我建议的两个选项很简单:

The two options I suggested, in brief:

  1. 在您要打开的工作簿中,将其添加到ThisWorkbook模块:

Public OpenedInCode as Boolean

您的呼叫工作簿需要打开工作簿,然后使用类似的东西

Your calling workbook needs to open the workbook, then use something like

With Workbooks("workbook name.xlsm")
   .OpenedInCode = True
   .RunAutoMacros xlAutoOpen
End With

然后,Auto_Open代码需要检查此值.如果它是False,则用户将其打开;否则,用户将其打开.如果是True,则其他工作簿将其打开.

Then the Auto_Open code needs to check this value. If it's False, the user opened it; if True, your other workbook opened it.

选项2只是使用Auto_Open中的代码创建另一个例程,该例程需要一个可选的布尔参数.然后,您的Auto_Open代码仅调用此例程.您的呼叫工作簿将使用Run

Option 2 is simply to create another routine with the code from your Auto_Open that takes an optional, say, Boolean parameter. Your Auto_Open code then just calls this routine. Your calling workbook would use Run

Dim wb as Workbook
Set wb = Workbooks.Open(path to file here)
Application.Run "'" & wb.Name & "'!routine_name", True

与选项1一样,被调用的例程仅检查参数是否为True或False并作出相应的反应.

As with option 1, the called routine just checks to see if the argument is True or False and reacts accordingly.

这篇关于用参数打开工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆