VBA Excel试图存储一个自定义类的数组

所以我一直在寻找stackoverflow,我打了一堵墙。 请原谅任何“不良行为”只需要这个逻辑运行一次。 这是我第一次在VBA中使用类(如果我有更多的时间,我会用C#写这个)

这是我的问题。 我有两个自定义类。 DocName和Doclib

Docname包含我的工作表中每行获得的各种信息。 我正在寻找“职员”的名字的独特性。 我无法做一个简单的search,因为我收到的这个数据库没有押韵的原因。 有时全名,有时与中间的首字母,有时倒退,有时与证书。 这是疯了。

缩写类如下:

Option Explicit Public staff As Boolean Public docID As Long Public namesCount As Integer Private names(5) As String Public CNumber As String Private fakeCnumber As Long Public rowIndex As Integer Public hasCnumber As Boolean Private rawString As String Private Sub class_initialize() namesCount = 0 hasCnumber = False rowIndex = 1 End Sub Public Sub resetClass() docID = 0 rowIndex = 1 resSize = 0 namesCount = 0 End Sub Public Function match(comp As DocName) As Boolean Dim goodMatch As Integer goodMatch = 0 Dim myNames As Integer Dim compNames As Integer myNames = 0 compNames = 0 While (nameValid(myNames)) While (comp.nameValid(compNames)) If names(myNames) = comp.getName(compNames) Then goodMatch = goodMatch + 1 End If compNames = compNames + 1 Wend myNames = myNames + 1 Wend If goodMatch > 1 Then match = True Else match = False End If End Function Public Function nameValid(i As Integer) As Boolean If i < namesCount Then nameValid = True If i >= namesCount Then nameValid = False End Function Public Property Let savestate(orig As DocName) Dim x As Integer x = 0 docID = orig.getdocID hasCnumber = orig.hasCnumber If hasCnumber Then CNumber = orig.getCNumber While orig.nameValid(x) names(x) = orig.getName(x) x = x + 1 Wend End Property 

我也有一个“DocLib”类。 该类的唯一目标是容纳每个我想保存的DocName实例。 我希望它在内存中完成一些后处理,然后再将其转储回Excel表。 下面再缩写一下:

 Option Explicit Private res(500) As DocName Private resSize As Integer Private Sub class_initialize() resSize = 0 End Sub Public Function addDoc(n As DocName) Dim x As Integer Dim m As Boolean m = True x = 0 While x < resSize & resSize > 0 If res(x).match(n) Then res(x).addOrder If res(x).hasCnumber = False & n.hasCnumber = True Then res(x).setCNumber = n.getCNumber End If m = False End If Wend If Not m Or resSize = 0 Then res(resSize) = New DocName res(resSize).savestate = n 'This is where it breaks ***** 'res(resSize) = n resSize = resSize + 1 End If End Function 

当我运行这个时,我得到错误91.对象variables或块variables未设置。 我在“Res(resSize).savestate = n”

我首先尝试了简单的作业,但是这给了我同样的错误。 所以我创造了没有变化的丰富function。 我也尝试给每个位置(崩溃前的行)分配一个新的文档。 我最初没有这样做,并且有同样的错误。

有任何想法吗? 提前致谢。 我相信这是一个简单的修复。 我只需要这个血腥的代码在testing集上工作一次,然后在20K集上再次工作,我不会再使用它。

  • 我试图改变索引从0到1在一开始没有工作。 当我通过debugging器进行跟踪时,在“new”语句之后,docname的class_initialize函数结束时会给出错误信息。 docname类可以在任何我需要的地方工作。 我尝试了一个简单的testing,我手动给它的行号,它的工作。 一旦我试图自己保存,它就打破了。

多谢你们! 〜乍得

res(ResSize)将存储对象的引用 – 实例化它,你需要使用

 Set res(resSize) = New DocName