2014-04-01 62 views
1

刪除使用VBA coding.I在用戶窗體子程序它存在於用戶窗體functionif試圖驗證碼刪除由VBA編碼

Sub DeleteProcedureCode(_ 
    ByVal DeleteFromModuleName As String, ByVal ProcedureName As String) 
' deletes ProcedureName from DeleteFromModuleName in wb 
Dim VBCM As CodeModule, ProcStartLine As Long, ProcLineCount As Long 
    On Error Resume Next 
    Set VBCM = ActiveWorkbook.VBProject.VBComponents(DeleteFromModuleName).CodeModule 
    If Not VBCM Is Nothing Then 
     ' determine if the procedure exist in the codemodule 
     ProcStartLine = 0 
     ProcStartLine = VBCM.ProcStartLine(ProcedureName, vbext_pk_Proc) 
     If ProcStartLine > 0 Then ' prosedyren finnes, slett den 
      ProcLineCount = VBCM.ProcCountLines(ProcedureName, vbext_pk_Proc) 
      VBCM.DeleteLines ProcStartLine, ProcLineCount 
     End If 
     Set VBCM = Nothing 
    End If 
    On Error GoTo 0 
End Sub 

但是,這將引發一個錯誤說用戶定義沒有定義

+2

爲什麼你需要刪除一個子?你不能簡單地忽略它嗎? –

回答

0

我不不知道如何刪除程序,但我可以給你類似的行爲:

Public lockedProcedures As Variant 

Sub lockProcedure(name As String) 
    If Not IsArray(lockedProcedures) Then 
     ReDim lockedProcedures(1) As String 
    Else 
     ReDim Preserve lockedProcedures(UBound(lockedProcedures) + 1) 
    End If 

    lockedProcedures(UBound(lockedProcedures)) = name 
End Sub 

Function isLocked(name As String) 
    If (UBound(Filter(lockedProcedures, name)) > -1) Then 
     MsgBox name & " is locked!" 
     isLocked = True 
    Else 
     isLocked = False 
    End If 
End Function 

Sub test() 
    Call lockProcedure("test2") 
End Sub 

Sub test2() 
    if not isLocked("test2") then 
     Code... 
    end if 
End Sub