通过变量传递到VBA中的sub [英] Passing through variable to sub in VBA

查看:46
本文介绍了通过变量传递到VBA中的sub的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是非常基本的,但是我尝试了很多事情,并且总是出错.

This is probably very basic, but I have tried so many things and the always give errors.

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

Basically what I'm trying to do is run a procedure every time a cell gets changed in Excel. So I'm using Sub Worksheet_Change(ByVal Target As Range). All works well, but in this procedure, I'm calling another sub procedure several times.

我想在此过程中重用'Target'值,但是由于某种原因,我找不到方法.我尝试将公共rTarget设置为范围",并在过程开始时执行"rTarget = Target".但是当我调用子过程时,rTarget保持为空.

I want to reuse the 'Target' value in this procedure, but for some reason, I can't find how to do it. I tried placing 'Public rTarget As Range' and do a 'rTarget = Target' at the beginning of the procedure. But the rTarget stays empty when I call the sub procedure.

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

How do I make this work? I'm now adding Target as one of the variables to to subroutine, but that just looks stupid.

谢谢!

推荐答案

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

这篇关于通过变量传递到VBA中的sub的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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