VBA数组学习笔记文档格式.docx

上传人:b****1 文档编号:14635044 上传时间:2022-10-23 格式:DOCX 页数:7 大小:16.96KB
下载 相关 举报
VBA数组学习笔记文档格式.docx_第1页
第1页 / 共7页
VBA数组学习笔记文档格式.docx_第2页
第2页 / 共7页
VBA数组学习笔记文档格式.docx_第3页
第3页 / 共7页
VBA数组学习笔记文档格式.docx_第4页
第4页 / 共7页
VBA数组学习笔记文档格式.docx_第5页
第5页 / 共7页
点击查看更多>>
下载资源
资源描述

VBA数组学习笔记文档格式.docx

《VBA数组学习笔记文档格式.docx》由会员分享,可在线阅读,更多相关《VBA数组学习笔记文档格式.docx(7页珍藏版)》请在冰豆网上搜索。

VBA数组学习笔记文档格式.docx

arr=[e22:

i24]

arr=Range(“e22:

i24”)

2.2,Array函数

myArray=Array("

AAA"

"

BBB"

200,500,"

2006-7-12"

如果代码头没有OptionBase1的语句,则数组myArray的上限为4,下限为0。

即下限LBound(myArr)=0,上限UBound(myArr)=4

二维数组的第一维的上限:

UBound(Arr,1)

二维数组的第二维的上限:

UBound(Arr,2)

多维数组上限的求法一样。

2.3,把单元格区域公式赋给数组

如果a5=B4+1

arr=[a4:

c8].Formula'

将单元格绝对引用公式保存到数组

[e4:

g8]=arr此时e5中的公式也=B4+1;

如果将单元格相对引用公式保存到数组

c8].FormulaR1C1

g8]=arr此时e5中的公式就=E4+1;

三、数组的处理

3.1,数组里的最大值和最小值

最大值aa=Application.WorksheetFunction.Max(Arr)

aa=Application.WorksheetFunction.Large(Arr,1)

最小值aa=Application.WorksheetFunction.Min(Arr)

aa=Application.WorksheetFunction.Small(Arr,1)

3.2,数组里搜索

Temp=Filter(Arr,xm(i))'

搜索数组

Subyy()

DimArr(),aa$,x%

aa="

asssfffssssaaasss"

:

bb="

s"

Forx=1ToLen(aa)

ReDimPreserveArr(1Tox)

Arr(x)=Mid(aa,x,1)

Nextx

temp=Filter(Arr,bb)

cc=UBound(temp)+1‘cc=”s”的个数

EndSub

用于对字符串数组进行搜索,得到一个新的数组temp,

缺点:

只告诉你某元素是否存在于数组中,而不知道其具体位置;

数组精确搜索:

SubFilterExactMatch()

'

该函数在一个字符串数组中搜索那些

与搜索字符串完全匹配的元素。

DimastrFilter()AsString

DimastrTemp()AsString

DimlngUpperAsLong

DimlngLowerAsLong

DimlngIndexAsLong

DimlngCountAsLong

astrItems=Array("

a"

sas"

Sas"

f"

strSearch="

为搜索字符串而过滤数组。

astrFilter=Filter(astrItems,strSearch)

存储结果数组的上限和下限。

lngUpper=UBound(astrFilter)

lngLower=LBound(astrFilter)

将临时数组调整到相同大小。

ReDimastrTemp(lngLowerTolngUpper)

在经过滤的数组的每个元素中循环。

ForlngIndex=lngLowerTolngUpper

检查该元素是否与搜索字符串完全匹配。

IfastrFilter(lngIndex)=strSearchThen

在另一个数组中存储完全匹配的元素。

astrTemp(lngCount)=strSearch

lngCount=lngCount+1

EndIf

NextlngIndex

重新调整包含完全匹配的元素的数组的大小。

ReDimPreserveastrTemp(lngLowerTolngCount-1)

返回包含完全匹配的元素的数组。

[a5].Resize(1,UBound(astrTemp)+1)=Application.Transpose(astrTemp)

3.3,转置

取工作表区域的转置到数组:

arr=Application.Transpose([a1:

c5])‘此时arr是转置成3行5列的数组,arr(1to3,1to5)

[e1:

i3]=arr‘此时3行5列。

数组间也可以转置:

arr1=Application.Transpose(arr)

取数组arr的第n列赋值到某列区域:

e5]=Application.Index(arr,0,n)

也可写成[e1:

e5]=Application.Index(arr,,n)

赋值产生一个新数组:

arr1=Application.Index(arr,0,n)

取数组arr的第n行赋值到某行区域:

[a6:

c6]=Application.Index(arr,n,0)

也可写成[a6:

c6]=Application.Index(arr,n)省略0,也省略了“,“

arr1=Application.Index(arr,n)

3.4,数组的比较(字典法)

题目:

将A列中的数据与C列相比较,输出C列中没有的数据到D列:

Subcc()

‘by:

ccwan

Dimarr,brr,i&

x&

dAsObject

arr=Range("

a1:

&

[a65536].End(xlUp).Row)

brr=Range("

c1:

c"

[c65536].End(xlUp).Row)

Setd=CreateObject("

scripting.dictionary"

Fori=1ToUBound(arr)

d(arr(i,1))="

"

Next

Forx=1ToUBound(brr)

Ifd.exists(brr(x,1))Then

d.Removebrr(x,1)

[d1].Resize(d.Count,1)=Application.Transpose(d.keys)

3.5,数组的排序

字符串数组不能用Large(Arr,i)或者Small(Arr,i)来排序;

但数值数组可以;

一个很好的字典+数组排序的实例:

Subyy1()

oobird

Dimi%,cAsRange,x,dAsObject

Setd=CreateObject("

Scripting.Dictionary"

ForEachcInSheet2.UsedRange

Ifc.Value<

>

"

Then

IfNotd.exists(c.Value)Then

d.Addc.Value,1

Else

d(c.Value)=d(c.Value)+1

Next

k=d.keys:

t=d.items'

k是各个不重复值,t是各个不重复值的个数

ReDimx(1To2,1Tod.Count)

Fori=1Tod.Count

x(2,i)=Application.Large(k,i)‘从大到小排序

x(1,i)=d(x(2,i))

Nexti

WithSheet1

.[b2].Resize(2,i-1)=x

x(1,i)=Application.Max(t)‘从大到小排序

w=Application.Match(x(1,i),t,0)–1‘查找此值在不重复值系列中的排位,因为w是从0开始的,所以-1

x(2,i)=k(w)‘求得对应的不重复值

t(w)="

‘使前面的最大值为空,继续循环

.[b5].Resize(2,i-1)=x‘两行一起赋值给B5开始的单元格

EndWith

字符串数组的排序,可以使用辅助列,把数组各元素依次赋给单元格,然后对这些单元格运用Excel自有的数据排序功能进行排序,再把单元格排过序的值重新赋给数组。

3.6,数组赋给单元格区域

r=Ubound(Arr)r为一维数组的上限;

Range("

a2"

).Resize(1,r)=Arr'

填充到工作表的一行之中(Arr为一维数组)

或者写成Range("

).Resize(1,Ubound(Arr))=Arr

二维数组Arr(100,5)

Range(“a1”).Resize(100,5)=Arr

[a1:

e100]=Arr

a1"

).Resize(Ubound(Arr,1),Ubound(Arr,2))=Arr

赋值方面的补充:

Suby()

Dimarr

arr=[mmult(row(1:

100),column(a:

f))]

[a1].Resize(100,6)=arr

arr=[column(a:

z)^3]

MsgBoxJoin(arr,"

"

arr=[transpose(row(1:

222))]

Subyyy()

arr=Split("

abcdefg"

.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1