在Excel VBA中将数组元素复制到数组元素 [英] Array Element to Array Element Copy in Excel VBA

查看:96
本文介绍了在Excel VBA中将数组元素复制到数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Excel VBA的新手,我来自SAS编程背景,这意味着我没有使用很多数组,因此几乎没有使用它的经验.所以请原谅我如果我弄错了任何术语.

I'm new to Excel VBA and I came from a SAS programming background meaning I didn't utilize a lot of arrays and thus have hardly any experience with it. So please, forgive me if I get any of the terminologies wrong.

我有2个动态数组,并希望基于这两个数组创建第三个数组,它们每个将具有相同的元素数,并且都是一维的.Array1将填充所有元素-但是Array2将填充一些元素,但就我们的目的而言,大多数为空白.只要Array2填充了一个元素,它将替换Array1上Array3上的任何元素编号.

I have 2 dynamic arrays and want to create a 3rd one based off these two and they each will have the same # of elements and will be all 1 dimensional. Array1 will have all elements filled - however Array2 will have some elements filled but are mostly blank for our purposes now. Whenever Array2 has an element filled, it will replace whatever Element number on Array1 for Array3.

我为所需的可视化截图了.

I made a screenshot for a visualization for what I'm looking for.

任何可以帮助我进一步研究的帮助或可能的功能都将有所帮助!

Any help or possible functions where I can look more into it would be helpful!

推荐答案

1.使用 ReDim 保留

2.声明 Array3 的大小: Max(Array1的大小,Array2的大小)

3.从Array2和Merge中检查 IsMissing IsEmpty

3.Check for IsMissing or IsEmpty values from Array2 and Merge

Sub MyMerger()
Dim Array1, Array2
Array1 = Array(5, 6, 7, 8, 9, 10, 11)    
Array2 = Array(, , 9, , , 2)    

ReDim Preserve Array2(UBound(Array1))    
ReDim Array3(WorksheetFunction.Max(UBound(Array1), UBound(Array2)))    

For i = 0 To UBound(Array3)
    If Not (IsMissing(Array2(i)) Or IsEmpty(Array2(i))) Then
    Array3(i) = Array2(i)
    Else
    Array3(i) = Array1(i)

    End If
Next i
End Sub

这篇关于在Excel VBA中将数组元素复制到数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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