2013-06-04 60 views
1

我有一个问题,我无法解决,直到现在。 我在窗体上放了一个按钮。如果不写代码。我可以点击它没有任何问题。如果我写了一些代码然后评论这段代码,我得到这个错误:Sub或函数未定义。我怎么解决这个问题?vba中的子​​功能或函数未定义错误

Private Sub Command40_Click() 
'Dim fso As New FileSystemObject 
'Dim ts As TextStream 
'Dim Name, Line As String 
'Dim regn As New regexp 
'Dim regx As New regexp 
'Dim regend As New regexp 
'Dim regxnum As New regexp 
'Dim swknnf As Boolean 
'Dim matchkennfeld, matchstx, matchend, matchxnum As MatchCollection 
' Name = util1.fDateiName("*.DCM", "Text") 
' Set ts = fso.OpenTextFile(Name, ForReading) 
'Do While Not ts.AtEndOfStream 
' set Line = ts.ReadLine 
'regn.Pattern = "KENNFELD\s+([A-Z 0-9]*)" 
    ' regx.Pattern = "\s*(ST/X)\s*([0-9]*\.[0-9]*\s*)+" 
    ' regxnum.Pattern = "\s*[0-9]*\.[0-9]*\s*" 
    ' regend.Pattern = "\s*(END)\s*" 
' Set matchkennfeld = regn.Execute(Line) 
' Set matchstx = regx.Execute(Line) 
' Set matchend = regend.Execute(Line) 
' If matchkennfeld.Count <> 0 Then 
' swknnf = True 
' End If 
' If matchend.Count <> 0 Then 
' swknnf = False 
' End If 
' If matchstx.Count <> 0 And swknnf = True Then 
' Set matchxnum = regxnum.Execute(Mid(Trim(matchstx.Item(0)), 5)) 
' For Each Match In matchxnum 
'  MsgBox Match 
' Next Match 
' End If 
'Loop 
End Sub 

回答

1

复制现有代码(仅限注释行)并将其粘贴到记事本中。在设计视图中打开您的表单。如果Property Sheet窗口已经打开,那么只需点击您的Command40按钮来选择它,否则右键单击您的按钮并选择“Properties”。

在属性页的“事件”选项卡上,删除On Click的任何现有值,然后单击省略号按钮(...)并选择“代码生成器”。这将在Sub处打开VBA编辑器,该编辑器现在定义为该按钮的.OnClick事件处理程序。将你的(注释掉的)代码粘贴在那里。

返回访问并验证按钮的On Click条目现在说[Event Procedure]。切换到窗体视图并点击按钮。此时它仍然不应该做任何事情(因为你的代码仍然被注释掉了),但它也不应该产生错误。