简体   繁体   English

使用变体数组加速每个循环。 Excel VBA

[英]Speed up for each loop with variant array. Excel VBA

I've got a worksheet that pulls a lot of data together from the rest of the workbook.我有一个工作表,它从工作簿的 rest 中收集了大量数据。 My code then runs a 'for each' loop to remove any spaces in the data.然后我的代码运行一个“for each”循环来删除数据中的任何空格。 The problem is i've got 60 instances of this code to run.问题是我要运行此代码的 60 个实例。 From named ranges A1 to A60.从命名范围 A1 到 A60。 My question is can i speed this section of the code up by using arrays?我的问题是我可以使用 arrays 加快这部分代码的速度吗? If i'm given pointers with this instance i can apply it to the other instances.如果我得到了这个实例的指针,我可以将它应用到其他实例。 I've no experience with arrays hence my need for help.我对 arrays 没有经验,因此我需要帮助。

在此处输入图像描述

I'd look into:我会调查:

A1.Value = Application.Substitute(A1, " ", "")

Or:或者:

A1.Replace what:=" ", Replacement:="", Lookat:=xlPart

Note that you can add more of the parameters of Range.Replace() to avoid any possible mishaps since this function will save and use settings of previous attempts using this function.请注意,您可以添加更多Range.Replace()参数以避免任何可能的事故,因为此 function 将保存并使用先前使用此 function 尝试的设置。

Like this:像这样:

Dim arr, r as long, v
'...
arr = A1.Value                 'get range values as array
for r = 1 to ubound(arr, 1)
    v = arr(r, 1)
    if len(v) > 0 Then arr(r, 1) = replace(v, " ", "")
next r
A1.Value = arr                 'return values to range

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM