excel - 在VBA中通过变量传递给子

标签 excel vba variables parameter-passing

这可能是非常基本的,但是我尝试了很多东西并且总是给出错误。

基本上我想要做的是每次在 Excel 中更改单元格时运行一个过程。所以我使用 Sub Worksheet_Change(ByVal Target As Range)。
一切正常,但在这个过程中,我多次调用另一个子过程。

我想在此过程中重用“目标”值,但由于某种原因,我找不到如何去做。我尝试将“Public rTarget As Range”放置在程序开始时并执行“rTarget = Target”。但是当我调用 sub 过程时 rTarget 保持为空。

我该如何进行这项工作?
我现在将 Target 作为变量之一添加到子例程中,但这看起来很愚蠢。

谢谢!

最佳答案

Private Sub Worksheet_Change(ByVal Target As Range)
    MySub Target
End Sub

Sub MySub(ByVal Target As Range)
    ' Your sub code goes here and can work with the Target Range from the Worksheet_Change Event
End Sub

关于excel - 在VBA中通过变量传递给子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16217537/

相关文章:

CommandBars 的 VB.NET 等效项

excel - 如何防止Excel在大文件上运行宏时崩溃?

Excel VBA 嵌套字典 - 访问项目

asp.net-mvc - 将 URL 中的问号和变量 (?id=5) 更改为 asp.net MVC 4 中的斜杠 (/5)

c# - 如何使用 DLL 在 C# 代码中读取 Excel 2010 文件?

excel - 使用 VBA 选择多个范围

vba - 查找不返回感兴趣的范围

excel - 使用 VBA 从表中复制和粘贴

mysql - 如何将变量传递给 IN 子句?

ruby-on-rails - rails : Submit Local Variables from view to another view