所以我一直在搜索stackoverflow,然后我撞墙了。请原谅任何“糟糕的做法”只需要这个逻辑运行一次。这是我第一次在VBA中使用类(如果我有更多的时间,我会用C#编写这个)
这是我的问题。我有两个自定义类。 DocName和Doclib
Docname包含我工作表中每行获得的各种信息。我正在寻找“职员”名字的独特性。我无法进行简单的搜索,因为我收到此文件的数据库没有押韵或理由如何存储名称。有时是全名,有时是初中,有时是后退,有时是凭证。这令人抓狂。
缩写类如下:
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.对象变量或未设置块变量。我在“Res(resSize).savestate = n”
上面的行中得到了这个我首先尝试了简单的任务,但这给了我同样的错误。所以我创建了savestate函数,没有任何变化。我还尝试为每个位置分配一个新的文档(崩溃前的行)。我最初没有这样做并且有同样的错误。
有什么想法吗?提前致谢。我确信这是一个简单的修复。我只需要这个血腥的代码在测试集上工作一次,然后再在20k集上工作,我将永远不再使用它。
谢谢你们! 〜乍得
答案 0 :(得分:2)
res(ResSize)
将存储对象的引用 - 要实例化它需要使用
Set res(resSize) = New DocName