如何实现自定义类型的动态数组?
Type aa<BR> name As String<BR> firstrow As Integer<BR> lastrow As Integer<BR> End Type<BR> Dim a() As aaReDim Preserve a(0 To 2)
'将a(0)~a(2)赋值,语句省略
addtype a, "ok"
Sub addtype(a, str)<BR> Dim i As Integer<BR> ReDim Preserve a(UBound(a) + 1)<BR> With a(UBound(a))<BR> .name = str<BR> .firstrow = a(UBound(a) - 1).lastrow<BR> .lastrow = .firstrow<BR> End With<BR>End Sub<BR>请帮忙解决以上代码的错误,实现能根据要求增加动态数组a()的功能 数组a未赋初始值!
Private Type aa<BR> name As String<BR> firstrow As Integer<BR> lastrow As Integer<BR> End Type
Private Sub Command1_Click()<BR> Dim a() As aa<BR> Dim i As Integer, k As Integer<BR> ReDim Preserve a(0)<BR> a(0).firstrow = 1<BR> a(0).lastrow = 2<BR> a(0).name = "ok"<BR> For i = 1 To 2<BR> ReDim Preserve a(i)<BR> With a(i)<BR> .name = "str"<BR> .firstrow = a(i - 1).lastrow<BR> .lastrow = .firstrow<BR> Debug.Print i, .name & "," & .firstrow & "," & .lastrow<BR> End With<BR> Next<BR>End Sub<BR> 赋过了,我写道省略.
问题是type放在sheet1中说不能,放在模块中则可以.然后重定义a()大小在sheet1中可以,而在模块中又不可以.该怎么办呢,type放在模块,重定义a()在sheet1中还是不行. 我有另一种方法:将用户定义类型改为类模块。作法:添加一个类模块vvvv,将Public name As String<BR>Public firstrow As Integer<BR>Public lastrow As Integer<BR>写入
引用时使用
dim a() as new vvvv 非得要占用一个类模块这么麻烦么?大家平时的自定义类型是怎么用的? 自定义类型怎么用,看我在2楼的贴。 请看3楼,我的type aa 要是全局变量.谢谢 注意a要定义为全局变量或窗体的模块级变量。
新建窗体、添加命令按钮。将下面代码放到窗体中。
Private Type aa<BR> name As String<BR> firstrow As Integer<BR> lastrow As Integer<BR>End Type<BR>Privatea() As aa
Private Sub addtype(str)<BR> Dim k As Integer<BR> k = UBound(a, 1)<BR> ReDim Preserve a(k + 1)<BR> k = UBound(a, 1)<BR> With a(k)<BR> .name = str<BR> .firstrow = a(k - 1).lastrow<BR> .lastrow = .firstrow<BR> End With<BR> Debug.Print k, a(k).name, a(k).firstrow, a(k).lastrow<BR> <BR>End Sub
<BR>Private Sub Command1_Click()<BR> <BR> ReDim a(0)
<BR> a(0).firstrow = 2<BR> a(0).lastrow = 2<BR> a(0).name = "ok"<BR> Dim i As Integer<BR> <BR> addtype "ok"<BR>End Sub my_computer 说的对,不过要把public 改为private type可以定义字符类型以外的类型吗?
页:
[1]