vba循環(huán)給數(shù)組賦值 Excel VBA如何刪除數(shù)組中重復(fù)的值?
Excel VBA如何刪除數(shù)組中重復(fù)的值?使用集合的唯一性鍵刪除重復(fù)值sub a()dim a(1到100)dim b as new collection dim I as integer a(1)=
Excel VBA如何刪除數(shù)組中重復(fù)的值?
使用集合的唯一性鍵刪除重復(fù)值sub a()dim a(1到100)dim b as new collection dim I as integer a(1)=1 a(2)=3 a(3)=5 a(4)=7 a(5)=3 a(6)=5 a(7)=2 on error resume next for I=1 to 100 b.為I=1 to b添加a(I),str(a(I))next I。計(jì)數(shù)a(I)=b(I)next iend sub
使用刪除重復(fù)值的集合的鍵唯一性suba()Dima(1to100)dimbas newcollection dimias Integera(1)=1a(2)=3A(3)=5A(4)=7a(5)=3A(6)=5A(7)=2oneresumenextfori=1to100b.ADDA(I),str(a(I))nextifori=1托布伯爵(i) =B(i)nextiensub
方法1
將a(9)作為整數(shù)
將B()作為整數(shù)
將i作為整數(shù),將j作為整數(shù)
重新定義保留B(0)
B(0)=a(0)
將i=1綁定到U(a)
將j=0綁定到U(B)
如果a(i)=B(j),則轉(zhuǎn)到網(wǎng)絡(luò),跳出如果你有相同的
下一步
ReDim preserve B(UBound(B)1)
B(UBound(B))=a(I)
net:跳轉(zhuǎn)到這里,匹配下一個(gè)數(shù)據(jù)
下一步
a=B]]方法2,有10個(gè)數(shù)字。首先找出最大數(shù),根據(jù)這個(gè)數(shù)定義一個(gè)數(shù)組,并將相應(yīng)的值寫(xiě)入相應(yīng)的元素中。最好先設(shè)置一個(gè)默認(rèn)值
將a(9)設(shè)置為整數(shù)
將I設(shè)置為整數(shù),將temp設(shè)置為整數(shù)
將temp=a(0)
將I=1設(shè)置為(a)左右
如果a(I)>,則將temp=a(I)
如果結(jié)束
下一個(gè)I
將B(temp)設(shè)置為整數(shù)
將I=0設(shè)置為(a)
B(a(I))=a(I)
下一個(gè)