VBA在用户窗体内调用私有函数 [英] VBA Calling a private function inside of a userform

查看:130
本文介绍了VBA在用户窗体内调用私有函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义为"SingleListForm"的用户窗体.我有一个私人子名为"loadSingleListForm(ID为Double)"我试图将变量传递到表单中,并且我以loadSingleListForm将基于ID设置表单的方式实现它,基本上是重用一种表单来显示列表框中来自变量源的变量数据.

I have a userform defined as "SingleListForm". I have a private sub named "loadSingleListForm(ID As Double)" I am attempting to pass a variable into the form and I was implementing it in the fashion that loadSingleListForm would set the form up based on ID, basically re-using one form to show variable data from variable sources in the listbox.

但是从表单(Worksheet_SelectionChange)外部调用标准的Intersect时,这两个选项可以编译,但是不起作用.

But calling a standard Intersect from outside the form (Worksheet_SelectionChange) these two options compile but do not work.

Application.Run "SingleListForm.loadSingleListForm", ID 'ID already declared and assigned

这也不起作用

Call ActiveWorkbook.UserForm("SingleListForm").loadSingleListForm(ID)

在说UserForm的地方,我也尝试过SingleListForm.这里的运行时错误是:

Where it says UserForm I have also tried SingleListForm. Here the runtime error is:

我正在努力不要在此处使用全局变量来传递给表单.也许我应该去初始化并在那里尝试一些事情.我试图将变量传递给表单,然后根据这种情况当然设置表单,然后显示表单.您无法通过演出,因此必须寻找另一种设置方式.

I am trying hard not to use a Global Variable here to pass to the form. Perhaps I should go to Initialize and try something there. I am trying to pass the variable to the form and then of course set up the form based on this case and then show the form. you can't pass with show so you have to find another way to set up.

我刚刚意识到我之前从未从窗体外部调用过用户窗体私有函数,但是我一直在使用模块来进行调用.第一种情况在这种情况下有效.

I just realized I have not called a userform private function from outside of the form before, but I do it with modules all the time. The first case works in that instance.

干杯,-WWC

推荐答案

执行此操作的更好方法是为表单声明一个属性.在表单的模块中输入

The better way to do this is to declare a property to the form. In the form's module enter

  Option Explicit
  Private myID as double

  Property Set ID(i as double)
     myID = i
  End Property

然后您的功能

   Private Sub loadSingleListForm()
     can refer to myID with in it's code

要从您使用的外部模块中使用此

To Use this from outside modules you use

   Load SingleListForm
   SingleListForm.ID = ID 'ID variable already declared

这篇关于VBA在用户窗体内调用私有函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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