EXCEL_VBA 如何添加数组元素

EXCEL_VBA 如何添加数组元素,第1张

Dim

a

......

ReDim

Preserve

a(i)

......

定义一个数组,如果比较后发现这个数为数组内没有,就让i=i+1后,重新定义一下数组,使其元素自动+1,再把值添加进去。

不明白的地方可以Q我,40194204

用For 循环就可以,例如,第一行有数据,用下程序就可以把A~F列的数据放到数据中。

Sub tt()

Dim Arry(50)

For i = 0 To 5

Arry(i) = Cells(1, i + 1)

Next

End Sub

sub bijiao()

dim a()

dim i as long

dim b

'定义数组

redim a(0 to 10)

'数组赋值

for i=0 to ubound(a)

a(i)=i

next

'b赋值

b=6

'循环比较

for i=0 to ubound(a)

if b=a(i) then exit for

next

'判断是否找到

if i>ubound(a) then

msgbox "没有找到"

else

redim preserve a(0 to ubound(a)+1)

a(ubound(a))=b

end if

end sub


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11590267.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存