VBA学习_2:数组:同类型的多个变量的集合

声明数组:名称、数据类型和数组大小

1、通过起始和终止索引号定义数组大小

  指定开始索引:Public | Dim 数组名称(a To b) As 数据类型, a和b为整数(不能是变量),分别表示数组的开始和终止索引号,可保存数据个个数(b-a+1)个

  未指定开始索引:Public | Dim数组名称(a) As 数据类型  ,从0开始到a个元素

Dim arr(1 To 100) As Byte   '定义一个Byte类型,名称为arr,大小为100的数组
Dim arr(99) As Byte   '定义一个Byte类型,名称为arr,大小为100的数组

2、给数组元素赋值

arr(20) = 56

声明多维数组

  指定开始索引:Public | Dim 数组名称(a To b,  x To y) As 数据类型  

  未指定开始索引:Public | Dim 数组名称(b,  y) As 数据类型   

  定义一个b行,y列的二维数组

声明动态数组

  Public | Dim 数组名称() As 数据类型 

重新定义数组大小

  ReDim 数组名称(1 To a)  只能定义大小不能修改数据类型 

创建数组的其他方法

1、使用Array函数创建数组

Sub arrytest()
    Dim arr As Variant
    arr = Array(1, 2, 3, 4)
    Msybos "arr数组的第二个元素为 : " & arr(1)
End Sub

2、使用Split函数分割字符串创建数组

Sub arrytest()
    Dim arr As Variant
    arr = Split("char,huang,car,box", ",") '第一参数是字符串,第二个参数是分割符
    Msybos "arr数组的第二个元素为 : " & arr(1)
End Sub

3、通过单元格区域创建数组(索引从1开始)

Sub arrytest()
    Dim arr As Variant
    arr = Range("A1:C3").Value '把A1:C3数据保存到数组
    Range("E1:G3").Value = arr '把数组数据保存到表格E1:G3中
    Msybos "arr数组的第二个元素为 : " & arr(1)
End Sub

数组的运算

1、UBound(数组名称,数组维度n) 数组n维度上的最大索引号(n可以缺默认第一维度)

Sub test()
    Dim arr(99) As Integer
    MsgBox UBound(arr)
End Sub

2、LBound(数组名称,数组维度n)数组n维度上的最小索引号(n可以缺默认第一维度)

Sub test()
    Dim arr(99) As Integer
    MsgBox LBound(arr)
End Sub

3、UBound(数组名称,数组维度n)-LBound(数组名称,数组维度n)+1 :n维度上数组的元素个数

4、Join(数组名称,拼接符号)函数把数组拼接为字符串(默认空格拼接)

5、把数组写入单元格区域

Range("A1").Value = arr(2)
Sub test()
    Dim arr As Variant '定义一个Variant类型的变量
    arr = Array(1, 2, 3, 4, 5)
    '把数组arr数据写入活动的工作表的A1:A5中
    Range("A1:A5").Value = Application.WorksheetFunction.Transpose(arr) 'Transpose把行数据转为列
End Sub

声明常量的同时给常量赋值

  过程内部使用Const(本地常量): Const 常量名称 As 常量类型 = 存储在常量中是数据

  模块的第一过程之前使用Const(模块级常量) :Const 常量名称 As 常量类型 = 存储在常量中是数据

  模块的第一个过程之前使用Public(公共常量):Public 常量名称 As 常量类型 = 存储在常量中是数据

转载于:https://www.cnblogs.com/jp-mao/p/9852033.html

Excel VBA常数变量集合,部分示例如下: Public Const xlAll = &HFFFFEFF8 Public Const xlAutomatic = &HFFFFEFF7 Public Const xlBoth = 1 Public Const xlCenter = &HFFFFEFF4 Public Const xlChecker = 9 Public Const xlCircle = 8 Public Const xlCorner = 2 Public Const xlCrissCross = 16 Public Const xlCross = 4 Public Const xlDiamond = 2 Public Const xlDistributed = &HFFFFEFEB Public Const xlDoubleAccounting = 5 Public Const xlFixedValue = 1 Public Const xlFormats = &HFFFFEFE6 Public Const xlGray16 = 17 Public Const xlGray8 = 18 Public Const xlGrid = 15 Public Const xlHigh = &HFFFFEFE1 Public Const xlInside = 2 Public Const xlJustify = &HFFFFEFDE Public Const xlLightDown = 13 Public Const xlLightHorizontal = 11 Public Const xlLightUp = 14 Public Const xlLightVertical = 12 Public Const xlLow = &HFFFFEFDA Public Const xlManual = &HFFFFEFD9 Public Const xlMinusValues = 3 Public Const xlModule = &HFFFFEFD3 Public Const xlNextToAxis = 4 Public Const xlNone = &HFFFFEFD2 Public Const xlNotes = &HFFFFEFD0 Public Const xlOff = &HFFFFEFCE Public Const xlOn = 1 Public Const xlPercent = 2 Public Const xlPlus = 9 Public Const xlPlusValues = 2 Public Const xlSemiGray75 = 10 Public Const xlShowLabel = 4 Public Const xlShowLabelAndPercent = 5 Public Const xlShowPercent = 3 Public Const xlShowValue = 2 Public Const xlSimple = &HFFFFEFC6 Public Const xlSingle = 2 Public Const xlSingleAccounting = 4 Public Const xlSolid = 1 Public Const xlSquare = 1 Public Const xlStar = 5 Public Const xlStError = 4 Public Const xlToolbarButton = 2 Public Const xlTriangle = 3 Public Const xlGray25 = &HFFFFEFE4 Public Const xlGray50 = &HFFFFEFE3 Public Const xlGray75 = &HFFFFEFE2 Public Const xlBottom = &HFFFFEFF5 Public Const xlLeft = &HFFFFEFDD Public Const xlRight = &HFFFFEFC8 Public Const xlTop = &HFFFFEFC0 Public Const xl3DBar = &HFFFFEFFD Public Const xl3DSurface = &HFFFFEFF9 Public Const xlBar = 2 Public Const xlColumn = 3 Public Const xlCombination = &HFFFFEFF1 Public Const xlCustom = &HFFFFEFEE Public Const xlDefaultAutoFormat = -1 Public Const xlMaximum = 2 Public Const xlMinimum = 4 Public Const xlOpaque = 3 Public Const xlTransparent = 2 Public Const xlBidi = &HFFFFEC78 Public Const xlLatin = &HFFFFEC77 Public Const xlContext = &HFFFFEC76 Public Const xlLTR = &HFFFFEC75 Public Const xlRTL = &HFFFFEC74 Public Const xlFullScript = 1 Public Const xlPartialScript = 2 Public Const xlMixedScript = 3 Public Const xlMixedAuthorizedScript = 4 Public Const xlVisualCursor = 2 Public Const xlLogicalCursor = 1 Public Const xlSystem = 1 Public Const xlPartial = 3 Public Const xlHindiNumerals = 3 Public Const xlBidiCalendar = 3 Public Const xlGregorian = 2 Public Const xlComplete = 4 Public Const xlScale = 3 Public Const xlClosed = 3 Public Const xlColor1 = 7 Public Const xlColor2 = 8 Public Const xlColor3 = 9 Public Const xlConstants = 2 Public Const xlContents = 2 Public Const xlBelow = 1 Public Const xlCascade = 7 Public Const xlCenterAcrossSelection = 7 Public Const xlChart4 = 2 Public Const xlChartSeries = 17 Public Const xlChartShort = 6 Public Const xlChartTitles = 18 Public Const xlClassic1 = 1 Public Const xlClassic2 = 2 Public Const xlClassic3 = 3 Public Const xl3DEffects1 = 13 Public Const xl3DEffects2 = 14 Public Const xlAbove = 0 Public Const xlAccounting1 = 4 Public Const xlAccounting2 = 5 Public Const xlAccounting3 = 6 Public Const xlAccounting4 = 17 Public Const xlAdd = 2 Public Const xlDebugCodePane = 13 Public Const xlDesktop = 9 Public Const xlDirect = 1 Public Const xlDivide = 5 Public Const xlDoubleClosed = 5 Public Const xlDoubleOpen = 4 Public Const xlDoubleQuote = 1 Public Const xlEntireChart = 20 Public Const xlExcelMenus = 1 Public Const xlExtended = 3 Public Const xlFill = 5 Public Const xlFirst = 0 Public Const xlFloating = 5 Public Const xlFormula = 5 Public Const xlGeneral = 1 Public Const xlGridline = 22 Public Const xlIcons = 1 Public Const xlImmediatePane = 12 Public Const xlInteger = 2 Public Const xlLast = 1 Public Const xlLastCell = 11 Public Const xlList1 = 10 Public Const xlList2 = 11 Public Const xlList3 = 12 Public Const xlLocalFormat1 = 15 Public Const xlLocalFormat2 = 16 Public Const xlLong = 3 Public Const xlLotusHelp = 2 Public Const xlMacrosheetCell = 7 Public Const xlMixed = 2 Public Const xlMultiply = 4 Public Const xlNarrow = 1 Public Const xlNoDocuments = 3 Public Const xlOpen = 2 Public Const xlOutside = 3 Public Const xlReference = 4 Public Const xlSemiautomatic = 2 Public Const xlShort = 1 Public Const xlSingleQuote = 2 Public Const xlStrict = 2 Public Const xlSubtract = 3 Public Const xlTextBox = 16 Public Const xlTiled = 1 Public Const xlTitleBar = 8 Public Const xlToolbar = 1 Public Const xlVisible = 12 Public Const xlWatchPane = 11 Public Const xlWide = 3 Public Const xlWorkbookTab = 6 Public Const xlWorksheet4 = 1 Public Const xlWorksheetCell = 3 Public Const xlWorksheetShort = 5 Public Const xlAllExceptBorders = 6 Public Const xlLeftToRight = 2 Public Const xlTopToBottom = 1 Public Const xlVeryHidden = 2 Public Const xlDrawingObject = 14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值