我正在尝试在VBA中编写一个可以将成员添加到excel的系统。它现在很简单,我真的无法弄清楚为什么我得到这个错误...我已经写了类似的子和函数来添加其他实体到我的数据库,他们没有工作没有问题...但是当我尝试添加一个新的“成员”我收到此错误消息“应用程序定义或对象定义错误”。这是代码,并提前感谢能够提供建议的任何人。
Sub Member_Sign_Up(MemberId As String)
Dim FirstName As String
Dim LastName As String
Dim PCode As String
Dim NumberOnLoan As String
Dim NextRM As Integer
With Range("A1:F1").Font
.Bold = True
End With
Worksheets("Members").Cells(1, 1) = "First Name" 'Set up the headings for the member fields
Worksheets("Members").Cells(1, 2) = "Last Name" 'Also set the font to bold
Worksheets("Members").Cells(1, 3) = "Post Code"
Worksheets("Members").Cells(1, 4) = "Number of Books on Loan"
Worksheets("Members").Cells(1, 5) = "Member ID"
Worksheets("Members").Cells(1, 6) = "Number of Members"
NextRM = Worksheets("Members").Cells(2, 6) + 2 'NextR is the next available row for new data (using the number of different Members to determine when the next free row is)
FirstName = InputBox("First Name?")
LastName = InputBox("Last Name?")
PCode = InputBox("Your Postcode (without spaces)?")
Worksheets("Members").Cells(2, 6) = Worksheets("Members").Cells(2, 6) + 1 'update the Members sheet "number of Members"
Worksheets("Members").Cells(NextRB, 1) = FirstName
Worksheets("Members").Cells(NextRB, 2) = LastName 'update the Members sheet
Worksheets("Members").Cells(NextRB, 3) = PCode
Worksheets("Members").Cells(NextRB, 5) = MemberId
End Sub
Sub Attempt_Sign_Up()
Dim i As Integer
Dim MemberId As String
MemberId = InputBox("Enter a prefered member ID, it must contain at least five letters and two numbers.")
If Not Check_Member_Exists(MemberId) Then 'And Check_MemberId_Valid(MemberId)
Member_Sign_Up (MemberId)
Else
MsgBox ("Member with ID " & MemberId & " is already taken or is not valid")
End If
End Sub
Function Check_Member_Exists(MemberId As String) As Boolean
Dim i As Integer
Dim Member_Found As Boolean
Member_Found = False 'Set Member_Found to false so that if it becomes true we know a member with that ID already exists
i = 0
Do 'Look through all the cells in Books worksheet with i as the index
i = i + 1
If Worksheets("Members").Cells(i, 6) = MemberId Then
Member_Found = True 'Check to see if we've found the ISBN
End If
Loop Until Member_Found Or i = Worksheets("Members").Cells(2, 6) + 2
Check_Member_Exists = Member_Found
End Function
答案 0 :(得分:3)
在更新会员表({{1}}等)时调用变量NextRM
的名称更改为NextRB
,这对我有用。