DeleteLines 方法

此頁(yè)沒(méi)有內(nèi)容條目
內(nèi)容

expandtri全部顯示

DeleteLines 方法從標(biāo)準(zhǔn)模塊類模塊中刪除代碼行。

expression.DeleteLines(StartLine, Count)

expression     必需。返回“應(yīng)用于”列表中的一個(gè)對(duì)象的表達(dá)式。

StartLine     必需 Long 型。Long 型值,指定要?jiǎng)h除的起始行的行號(hào)。

Count     必需 Long 型。Long 型值,指定要?jiǎng)h除的行數(shù)。

說(shuō)明

模塊中行的編號(hào)從 1 開始。若要確定模塊中的行數(shù),請(qǐng)使用 CountOfLines 屬性。

若要以一行替換另一行,請(qǐng)使用 ReplaceLine 方法。

示例

下面的示例從模塊中刪除指定的行:

Function DeleteWholeLine(strModuleName, strText As String) _

     As Boolean

    Dim mdl As Module, lngNumLines As Long

    Dim lngSLine As Long, lngSCol As Long

    Dim lngELine As Long, lngECol As Long

    Dim strTemp As String

    On Error GoTo Error_DeleteWholeLine

    DoCmd.OpenModule strModuleName

    Set mdl = Modules(strModuleName)

    If mdl.Find(strText, lngSLine, lngSCol, lngELine, lngECol) Then

        lngNumLines = Abs(lngELine - lngSLine) + 1

        strTemp = LTrim$(mdl.Lines(lngSLine, lngNumLines))

        strTemp = RTrim$(strTemp)

        If strTemp = strText Then

            mdl.DeleteLines lngSLine, lngNumLines

        Else

            MsgBox "Line contains text in addition to '" _

                & strText & "'."

        End If

    Else

        MsgBox "Text '" & strText & "' not found."

    End If

    DeleteWholeLine = True

Exit_DeleteWholeLine:

    Exit Function

Error_DeleteWholeLine:

    MsgBox Err & " :" & Err.Description

    DeleteWholeLine = False

    Resume Exit_DeleteWholeLine

End Function

可以從以下過(guò)程中調(diào)用該函數(shù),從而在模塊“模塊1”中搜索常量的聲明語(yǔ)句并將其刪除。

Sub DeletePiConst()

    If DeleteWholeLine("Module1", "Const conPi = 3.14") Then

        Debug.Print "Constant declaration deleted successfully."

    Else

        Debug.Print "Constant declaration not deleted."

    End If

End Sub