我在 excel 中使用 VBA 来创建一个 testreport 数据库。当我引用一个单元格来查找文档编号时,我收到一个错误script out of range (Error 9)
。
我正在使用的代码是:
LookUpRowCounter = HeaderRow + 1
Do Until Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = ""
If Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = DocumentNumber Then
Exit Do
End If
LookUpRowCounter = LookUpRowCounter + 1
Loop
错误发生在第一条记录的 If 语句中,其中计数器 =5。数据表“列表”中从第 5 行到第 15 行有 10 条记录。
任何帮助表示赞赏
编辑
文档编号的格式为 0000AA000,包含数字和大写字母。
Public Sub Archive()
'On Error GoTo Err
Dim DocumentNumber As String
Dim ProjectNumber As Single
Dim DBName As String
Dim DBLocation As String
Dim LookUpRowCounter As Single
Application.ScreenUpdating = False
DBName = "Attribute DataSheet.xls"
DBLocation = "J:\home\PEJ2WO\Database For Martin\"
DocumentNumber = ThisWorkbook.Sheets("Detail and Summary").Range("infDocumentNumber").Text
Workbooks.Open Filename:=DBLocation & DBName
If Not DocumentNumber = "" Then
'Document number present
LookUpRowCounter = HeaderRow + 1
Do Until Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = DocumentNumber
If Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = "" Then Exit Do
LookUpRowCounter = LookUpRowCounter + 1
Loop
Else
'create new document number
DocumentNumber = GetDocumentNumbers(DocumentNumber)
ThisWorkbook.Sheets("Detail and Summary").Unprotect (Password)
ThisWorkbook.Sheets("Detail and Summary").Range("infDocumentNumber").Value = DocumentNumber
'ThisWorkbook.Sheets("Detail And Summary").Range("infProjectNumber").Value = ProjectNumber
ThisWorkbook.Sheets("Detail And Summary").Protect (Password)
LookUpRowCounter = HeaderRow + 1
Do Until Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = ""
If Workbooks(DBName).Worksheets("List").Cells(LookUpRowCounter, 1).Text = DocumentNumber Then
Exit Do
End If
LookUpRowCounter = LookUpRowCounter + 1
Loop
End If
在这一点之后,只有将值写入摘要表的代码,这是冗长的,不应该相关。