Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance excel性能使用范围_Performance_Excel_Vba - Fatal编程技术网

Performance excel性能使用范围

Performance excel性能使用范围,performance,excel,vba,Performance,Excel,Vba,我是VBA的新手,现在正在从事一个项目,速度是一切。因此,在编写代码时,我注意到工作表中的许多单元格都是命名范围,并在函数中显式引用,如下所示: function a() if range("x") > range("y") then end if ... (just imagine a lot of named ranges) end function 'i can pass in the correct cells when i call the f

我是VBA的新手,现在正在从事一个项目,速度是一切。因此,在编写代码时,我注意到工作表中的许多单元格都是命名范围,并在函数中显式引用,如下所示:

function a() 

    if range("x") > range("y") then

    end if

    ... (just imagine a lot of named ranges)

end function
'i can pass in the correct cells when i call the function
function a(x as int, y as int) 

    if x > y then

    end if

    ...

end function
我的问题是,我是否应该修改这些函数,以便将这些命名范围中的值作为如下参数传入:

function a() 

    if range("x") > range("y") then

    end if

    ... (just imagine a lot of named ranges)

end function
'i can pass in the correct cells when i call the function
function a(x as int, y as int) 

    if x > y then

    end if

    ...

end function

这会使事情加快一点吗?为了与RTD服务器进行通信,这些函数几乎经常被调用(除非进程故意处于休眠状态)。VBA与工作表建立“连接”的速度要比处理自己的变量慢得多。如果函数多次引用同一单元格(或区域),则在VBA与它们交互之前将其加载到内存中是有利的。例如,如果
range(“x”)>range(“y”)
是函数中唯一引用
x
y
的时间,则这无关紧要。如果你有
If-range(“x”)>range(“a”)
If-range(“x”)>range(“b”)等等,那么你最好从

varX=range("x")
varY=range("y") 
然后使用VBA变量

通过第二个示例所示的函数参数化,似乎可以实现我的建议。这可能是,也可能不是,因为Excel可能只是将这些变量视为工作表的引用,而不是值(我不确定)。为了安全起见,您应该在函数的开头明确定义新变量,然后只在函数的其余部分引用这些变量


总而言之,您的目标应该是尽量减少VBA“连接”到工作表的次数

不完全是这样,但如果您可以将所有范围加载到一个数组中,并在数组中进行比较,则速度可能会更快。数组处理肯定比在一个范围内循环快,但我不确定这是否适用于您的情况。不,没有范围块。它们基本上是位于表中所有位置的所有单独开关。那么,你是在建议使用参数是一种更好的方法吗?我不是在建议,而是其他不适用的方法。这听起来像是一个功能将有助于组织这些东西在各地,但它不会帮助速度。