Excel vba变体数组可重复使用,必须删除吗? [英] Excel vba variant array reusable, must be erased?

查看:33
本文介绍了Excel vba变体数组可重复使用,必须删除吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个技术问题:

我的问题:我创建

Dim arrTemp as Variant 
Dim wbSource as workbook
Dim wbTarget as workbook

因为我需要将多个范围从多个工作表(非固定范围)导出到另一个工作簿.我的代码如下:

because I need to export multiple ranges from multiple worksheets (not fixed range) to another workbook. My code looks like:

' Worksheet 1
arrTemp = wbSource(1).Range("A1:B2").value
wbTarget(1).Range("A1:B2").value = arrTemp
If Not(IsArrayEmpty(arrTemp)) Then Erase arrTemp
' Worksheet 2
arrTemp = wbSource(2).Range("A1:B2").value
wbTarget(2).Range("A1:B2").value = arrTemp
If Not(IsArrayEmpty(arrTemp)) Then Erase arrTemp
' Worksheet 3
arrTemp = wbSource(3).Range("A1:B2").value
wbTarget(3).Range("A1:B2").value = arrTemp
If Not(IsArrayEmpty(arrTemp)) Then Erase arrTemp

(工作表首先可以是空的,这就是为什么空arr处理程序的原因)(工作表可以包含int/str/double/...,并且大小不足以定义特定的arr类型)

(worksheet can be empty in the first place, that's why empty arr handler) (worksheets can contain int/str/double/... and the size is not that big to define specific arr type)

我的问题是:每次删除阵列是否有意义?还是会被自动覆盖?

My question is: Does it make sense to erase the array every time? or It will be overwritten automatically?

在定义具有新范围的数组之前和之后,我进行了一项测试以检查数组的属性(Lbound和ampound).我可以看到它会自动重新调整数组的大小.这是否意味着我只需要在过程结束时清除它?

I did a test to check the properties of the array (Lbound & UBound) before and after defining the array with a new range. I can see that It automatically Redim the array. Does it means that I only need to clear it in the end of the procedure?

还是在两者之间清除它是一个好习惯?

Or it is a good practice to clear it in between?

最后但并非最不重要的一点是,您在我的代码中看到任何问题吗?更好的方法来执行此任务?

Last but not least, do you see any problem in my code? Better way to perform this task?

非常感谢!

记住该代码不适合此任务,无需传输到数组!

Bear in mind The code is not correct for this task, no need to transfer to an array!

推荐答案

简而言之(感谢rory!):

In Short (thanks to rory!):

之间无需每次都擦除阵列.仅在离开程序之前.

No need to erase the array every time in between. Only before leaving the procedure.

这篇关于Excel vba变体数组可重复使用,必须删除吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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