如何在vba动态数组不清空维数前提下快速将所有元素赋值为0,vba erasee会把动态数组维数清空

vba如何定义动态多维数组_excel吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:137,273贴子:
vba如何定义动态多维数组收藏
arr = Sheets(&产品配比表汇总&).Range(&A2:J& & Sheets(&产品配比表汇总&).Cells(Rows.Count, 1).End(xlUp).Row)ReDim brr(1 to 1, 1 To 11)DTSZ = 1
For i = 1 To UBound(arr)
If arr(i, 1) = Target.Value Then
ReDim Preserve brr(1 To DTSZ, 1 To 11)
brr(DTSZ, 1) = arr(i, 1)
DTSZ = DTSZ + 1
Next i我想定义一个多维的BRR,但是总是下标越界,请老师指点!
京东电脑节,全民抢宝进行时!1999抢i7本,半价秒电脑,抢直降3000显示器
redim preserve 只能改变数组的最后一维,redim preserve brr(1 to 11,1 to dtsz)
Redim帮助文件内容:如果使用了 Preserve 关键字,就只能重定义数组最末维的大小,且根本不能改变维数的数目。例如,如果数组就是一维的,则可以重定义该维的大小,因为它是最末维,也是仅有的一维。不过,如果数组是二维或更多维时,则只有改变其最末维才能同时仍保留数组中的内容。下面的示例介绍了如何在为已有的动态数组增加其最末维大小的同时而不清除其中所含的任何数据。因为前面有了ReDim brr(1 to 1, 1 To 11)所有ReDim Preserve brr(1 To DTSZ, 1 To 11)是不允许的ReDim Preserve brr(1 To 1, 1 To 100000)是可以的
Function Index2(trr_Array_Area, Optional r_RowIndex = 0, Optional c_ColumnIndex = 0, Optional k_LBound_Transpose = 0, Optional h_RowHeight& = -1, Optional w_ColumnWidth& = -1)
Rem 兼具 Index数组提取、数组Redim、数组Transpose转置 三大功能的 自定义函数
Rem Index除可提取整行、整列外 更可提取任意行列位置起始的多行多列矩形局域 并可任意设置数组下标开始值(或保留原始值)
Rem Redim则很简单 Index提取时按需要重新设置行高、列宽即可 优点是可以同时设置二维数组的2个维度 而标准Redim只能修改第2维的大小
Rem Transpose同样简单 但由于兼具上述特点而更强大 1.任意行列位置开始 2.任意二维大小Redim 3.无65536限制
Rem 第1参数trr_Array_Area: 为引用的VBA内存一维或二维数组如arr 或[工作表区域].Value的二维结构数组
Rem 第2、3参数r_RowIndex 和 第3参数c_ColumnIndex:可省略。默认值=0即整行、整列,否则为数组起点开始的行、列相对位置
Rem 第4参数k_LBound_Transpose:
该参数可省略。 默认值=0即设置新数组起点开始LBound=0 否则如果是数值则按指定数值开始 数值应该是含0整数
该参数为空值=&&时 按原数组指定行列开始的位置作为新数组起点开始的LBound值
该参数首字母=&T&时 除Index、Redim功能外 还对数组结果进行Transpose的行列转置 但转置结果仍是二维数组
该参数首字母=&T&时 其后的数值仍可作为新数组起点开始的LBound值 因此=&T&时相当于=&T0&则新数组起点开始LBound=0
Rem 第5、6参数h_RowHeight 和 w_ColumnWidth:该参数可省略。 默认值=-1即输出一维数组 否则按指定值进行多行、多列的二维数组输出
Dim r1_RowStart&, c1_ColumnStart&, r2_RowEnd&, c2_ColumnEnd&, kr_RowLBound&, kc_ColumnLBound&, i_RowCount&, j_ColumnCount&, d_OneDimensionArray&
On Error GoTo 1
c1_ColumnStart = LBound(trr_Array_Area, 2)
If r_RowIndex = 0 Then r1_RowStart = LBound(trr_Array_Area) Else r1_RowStart = LBound(trr_Array_Area) + r_RowIndex - 1
If c_ColumnIndex = 0 Then c1_ColumnStart = LBound(trr_Array_Area, 2) Else c1_ColumnStart = LBound(trr_Array_Area, 2) + c_ColumnIndex - 1
d_OneDimensionArray = 1
If r_RowIndex = 0 Then c1_ColumnStart = 0 Else If c_ColumnIndex = 0 Then c_ColumnIndex = r_RowIndex: r_RowIndex = 0
If c_ColumnIndex = 0 Then r1_RowStart = LBound(trr_Array_Area) Else r1_RowStart = LBound(trr_Array_Area) + c_ColumnIndex - 12
If r_RowIndex & 0 And h_RowHeight = -1 Then
If k_LBound_Transpose = && Then kc_ColumnLBound = c1_ColumnStart Else kc_ColumnLBound = k_LBound_Transpose
If d_OneDimensionArray = 0 Then c2_ColumnEnd = UBound(trr_Array_Area, 2)
If w_ColumnWidth & 0 Then If c1_ColumnStart + w_ColumnWidth - 1 & c2_ColumnEnd Then c2_ColumnEnd = c1_ColumnStart + w_ColumnWidth - 1
ReDim tr_Output(kc_ColumnLBound To c2_ColumnEnd - c1_ColumnStart + kc_ColumnLBound)
For j_ColumnCount = c1_ColumnStart To c2_ColumnEnd
tr_Output(j_ColumnCount - c1_ColumnStart + kc_ColumnLBound) = trr_Array_Area(r1_RowStart, j_ColumnCount)
Index2 = tr_Output
ElseIf c_ColumnIndex & 0 And w_ColumnWidth = -1 Then
If k_LBound_Transpose = && Then kr_RowLBound = r1_RowStart Else kr_RowLBound = k_LBound_Transpose
r2_RowEnd = UBound(trr_Array_Area)
If h_RowHeight & 0 Then If r1_RowStart + h_RowHeight - 1 & r2_RowEnd Then r2_RowEnd = r1_RowStart + h_RowHeight - 1
ReDim tr_Output(kr_RowLBound To r2_RowEnd - r1_RowStart + kr_RowLBound)
If d_OneDimensionArray = 1 Then
For i_RowCount = r1_RowStart To r2_RowEnd
tr_Output(i_RowCount - r1_RowStart + kr_RowLBound) = trr_Array_Area(i_RowCount)
For i_RowCount = r1_RowStart To r2_RowEnd
tr_Output(i_RowCount - r1_RowStart + kr_RowLBound) = trr_Array_Area(i_RowCount, c1_ColumnStart)
Index2 = tr_Output
If k_LBound_Transpose = && Then
kr_RowLBound = r1_RowStart: kc_ColumnLBound = c1_ColumnStart
ElseIf k_LBound_Transpose Like &T*& Then
If k_LBound_Transpose = &T& Then
kr_RowLBound = r1_RowStart: kc_ColumnLBound = c1_ColumnStart
kr_RowLBound = Val(Mid(k_LBound_Transpose, 2)): kc_ColumnLBound = kr_RowLBound
kr_RowLBound = k_LBound_Transpose: kc_ColumnLBound = k_LBound_Transpose
If h_RowHeight & 0 Then r2_RowEnd = r1_RowStart + h_RowHeight - 1 Else r2_RowEnd = UBound(trr_Array_Area)
If d_OneDimensionArray = 0 Then If w_ColumnWidth & 0 Then c2_ColumnEnd = c1_ColumnStart + w_ColumnWidth - 1 Else c2_ColumnEnd = UBound(trr_Array_Area, 2)
If k_LBound_Transpose Like &T*& Then
ReDim tr2_Output(kc_ColumnLBound To c2_ColumnEnd - c1_ColumnStart + kc_ColumnLBound, kr_RowLBound To r2_RowEnd - r1_RowStart + kr_RowLBound)
If r2_RowEnd & UBound(trr_Array_Area) Then r2_RowEnd = UBound(trr_Array_Area)
If d_OneDimensionArray = 0 Then If c2_ColumnEnd & UBound(trr_Array_Area, 2) Then c2_ColumnEnd = UBound(trr_Array_Area, 2)
If d_OneDimensionArray = 1 Then
For i_RowCount = r1_RowStart To r2_RowEnd
tr2_Output(j_ColumnCount - c1_ColumnStart + kc_ColumnLBound, i_RowCount - r1_RowStart + kr_RowLBound) = trr_Array_Area(i_RowCount)
For i_RowCount = r1_RowStart To r2_RowEnd
For j_ColumnCount = c1_ColumnStart To c2_ColumnEnd
tr2_Output(j_ColumnCount - c1_ColumnStart + kc_ColumnLBound, i_RowCount - r1_RowStart + kr_RowLBound) = trr_Array_Area(i_RowCount, j_ColumnCount)
ReDim tr2_Output(kr_RowLBound To r2_RowEnd - r1_RowStart + kr_RowLBound, kc_ColumnLBound To c2_ColumnEnd - c1_ColumnStart + kc_ColumnLBound)
If r2_RowEnd & UBound(trr_Array_Area) Then r2_RowEnd = UBound(trr_Array_Area)
If d_OneDimensionArray = 0 Then If c2_ColumnEnd & UBound(trr_Array_Area, 2) Then c2_ColumnEnd = UBound(trr_Array_Area, 2)
If d_OneDimensionArray = 1 Then
For i_RowCount = r1_RowStart To r2_RowEnd
tr2_Output(i_RowCount - r1_RowStart + kr_RowLBound, j_ColumnCount - c1_ColumnStart + kc_ColumnLBound) = trr_Array_Area(i_RowCount)
For i_RowCount = r1_RowStart To r2_RowEnd
For j_ColumnCount = c1_ColumnStart To c2_ColumnEnd
tr2_Output(i_RowCount - r1_RowStart + kr_RowLBound, j_ColumnCount - c1_ColumnStart + kc_ColumnLBound) = trr_Array_Area(i_RowCount, j_ColumnCount)
Index2 = tr2_Output
End IfEnd Function不是我写的,香川群子写的,用起来挺方便,我经常用.
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Windows10用户联盟QQ群:
什么是数组?
我们非常清楚地知道,一个变量是一个容器来存储值。有时开发者在一个位置,在一个单一的变量一次持有多个值。当一系列值被存储在一个单独的变量,那么它被称为数组变量。
数组声明以其它变量的方式同样,只是数组变量的声明使用圆括号声明。在下面的例子中,数组大小在括号中指定。
'Method 1 : Using Dim
Dim arr1() 'Without Size
'Method 2 : Mentioning the Size
Dim arr2(5)
'Declared with size of 5
'Method 3 : using 'Array' Parameter
arr3 = Array(&apple&,&Orange&,&Grapes&)
虽然,数组大小显示为5,它可以容纳6个值作为数组索引从零开始。
数组索引不能为负数。
VBScript数组可以存储任何类型的变量数组。因此,一个阵列可以存储的整数,串或字符在一个单一的数组变量。
数值通过指定数组索引值对值中的每一个将被分配被分配到阵列。它可以是一个字符串。
添加一个按钮,并添加以下功能
Private Sub Constant_demo_Click()
Dim arr(5)
arr(0) = &1&
'Number as String
arr(1) = &VBScript&
'String
arr(2) = 100
'Number
arr(3) = 2.45
'Decimal Number
arr(4) = #10/07/2013#
arr(5) = #12.45 PM#
msgbox(&Value stored in Array index 0 : & & arr(0))
msgbox(&Value stored in Array index 1 : & & arr(1))
msgbox(&Value stored in Array index 2 : & & arr(2))
msgbox(&Value stored in Array index 3 : & & arr(3))
msgbox(&Value stored in Array index 4 : & & arr(4))
msgbox(&Value stored in Array index 5 : & & arr(5))
当执行函数输出如下所示:
Value stored in Array index 0 : 1
Value stored in Array index 1 : VBScript
Value stored in Array index 2 : 100
Value stored in Array index 3 : 2.45
Value stored in Array index 4 : 7/10/2013
Value stored in Array index 5 : 12:45:00 PM
数组并不仅仅局限于单一的维度,最多可有60维度。最常用的是二维数组。
在下面的例子中,一个多维阵列具有3行和4列声明。
Private Sub Constant_demo_Click()
Dim arr(2,3) as Variant ' Which has 3 rows and 4 columns
arr(0,0) = &Apple&
arr(0,1) = &Orange&
arr(0,2) = &Grapes&
arr(0,3) = &pineapple&
arr(1,0) = &cucumber&
arr(1,1) = &beans&
arr(1,2) = &carrot&
arr(1,3) = &tomato&
arr(2,0) = &potato&
arr(2,1) = &sandwitch&
arr(2,2) = &coffee&
arr(2,3) = &nuts&
msgbox(&Value in Array index 0,1 : & &
msgbox(&Value in Array index 2,2 : & &
当执行函数输出如下所示:
Value stored in Array index : 0 , 1 : Orange
Value stored in Array index : 2 , 2 : coffee
Redim 语句
ReDim语句用于声明动态数组变量并分配或重新分配存储空间。
ReDim [Preserve] varname(subscripts) [, varname(subscripts)]
Preserve - 可选参数,用来当改变最后一维的大小来保存数据在现有的数组。
varname - 必需的参数,它表示的变量,它应该遵循标准的变量命名约定的名称。
subscripts - 必需的参数,它表示该数组大小。
在下面的例子中一个数组已经被重新定义,在保存的值时该数组的现有大小被改变。
注:在调整大小的数组小于它最初的值,在消除元素的数据将会丢失。
Private Sub Constant_demo_Click()
Dim a() as variant
redim a(5)
a(0)=&XYZ&
a(1)=41.25
REDIM PRESERVE a(7)
For i=3 to 7
'to Fetch the output
For i=0 to ubound(a)
Msgbox a(i)
当执行函数输出如下所示:
数组方法:
在VBScript中的各种内置函数,帮助开发者有效地处理数组。所有正在使用中一起选择数组方法在下面列出。请点击方法名详细了解。
此函数返回一个整数,对应于给定的数组中最小的下标。
此函数返回一个整数,对应于给定数组的最大下标。
此函数返回包含值的指定数量的数组。分割后基于分隔符。
此函数返回一个包含子字符串数组中的指定数量的字符串。这是Split 方法的完全相反的作用。
此函数返回零的数组包含字符串数组基于一个特定的过滤标准的子集。
此函数返回一个布尔值,指示输入变量是否是一个数组。
此函数恢复所分配的内存为数组变量。
标签:&&&&&&&&&&&&VBA动态数组 - 不小的文 - 博客园
随笔 - 16, 文章 - 1, 评论 - 0, 引用 - 0
如何动态地向数组中添加新的数据,可以用动态数组实现。
dim arr() as string '定义动态数组,这里不能用variant类型,不然后面用redim会出错
redim preserve arr(row,col) as string 'redim就是起动态定义的作用,因为这里row和col可以是变量,dim中必须是常量。preserve按列保留数组中已有的数据,所有加preserve之后只能按列添加数据
二维数组的赋值则用一组循环语句进行赋值,不能按列或行整体赋值。
另外编程中一定要考虑极限情况,为空或者全选等等基本情况。温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
出生于长乐山脚下某一乡村。祖祖辈辈以务农为生。
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(916)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'VBA-语句:Erase、Error、Event',
blogAbstract:'&
E:\\VBA\\0421,语句06.txt
16,Erase 语句&&&&& 1601,重新初始化大小固定的数组的元素,以及释放动态数组的存储空间。1602,语法:Erase arraylist&&&&&&&&&& &所需的 arraylist 参数是一个或多个用逗号隔开的需要清除的数组变量。1603,说明:&&&&&&&&&& &Erase 根据是固定大小(常规的)数组还是动态数组,来采取完全不同的行为。Erase 无需为固定大小的数组恢复内存。Erase 按下表来设置固定数组的元素:',
blogTag:'vba,erase,error,event,语句',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:0,
permalink:'blog/static/',
commentCount:4,
mainCommentCount:2,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'出生于长乐山脚下某一乡村。祖祖辈辈以务农为生。',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}

我要回帖

更多关于 excel vba 单元格赋值 的文章

 

随机推荐