VBA立即窗口与应用程序运行时有何不同? [英] How does the VBA immediate window differ from the application runtime?
问题描述
我在VBA中遇到了一个非常奇怪的错误,想知道是否有人可以透露一些信息?
I've encountered a very strange bug in VBA and wondered if anyone could shed some light?
我正在这样调用工作表函数:
I'm calling a worksheet function like this:
Dim lMyRow As Long
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
这旨在获取我传递的项目的行.在某些情况下(尽管我无法确切确定何时),对Match函数的调用发生了奇怪的事情.
This is intended to get the row of the item I pass in. Under certain circumstances (although I can't pin down exactly when), odd things happen to the call to the Match function.
如果在立即窗口中执行该行,则会得到以下信息:
If I execute that line in the immediate window, I get the following:
lMyRow = WorksheetFunction.Match(vItemID, rngMyRange.Columns(1), 0)
?lMyRow
10
即查找有效,lMyRow获得分配给它的值.如果我让该语句在实际代码中执行,我的lMyRow值将为0.
i.e. the lookup works, and lMyRow gets a value assigned to it. If I let that statement execute in the actual code, I lMyRow gets a value of 0.
这似乎很奇怪!我不明白在即时窗口中执行某项操作如何成功分配一个值,在正常执行代码的情况下,在程序执行的同一点进行相同的调用时,该值可以为0!
This seems very odd! I don't understand how executing something in the immediate window can succeed in assigning a value, where the same call, at the same point in program execution can give a value of 0 when it runs normally in code!
我唯一能想到的是它是某种奇怪的强制转换,但是如果我要分配给它的变量是一个整数,一个双精度甚至是一个字符串,我都会得到相同的行为.
The only thing I can think of is that it's some odd casting thing, but I get the same behaviour taking if the variable to which I'm assigning is an int, a double, or even a string.
我什至不知道从哪里开始-帮助!
I don't even know where to begin with this - help!!
推荐答案
您没有分配函数名称,因此该函数将始终返回零(如果您期望使用Long).看来您应该拥有
You're not assigning the function name so that function will always return zero (if you're expecting a Long). It seems you should have
makeTheLookup = lMyRow
在功能末尾.
这篇关于VBA立即窗口与应用程序运行时有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!