vba 为下一个循环清除/清空变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37235599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:25:10  来源:igfitidea点击:

Clear / Empty Variable for next loop

vbams-wordword-vbaword-2007

提问by Ibn e Ashiq

I am having a situation in which I need to clearthe variable lastWordso that msgboxshow nothing in next loop.

我遇到了一种情况,我需要clear变量lastWord以便msgbox在 next 中不显示任何内容loop

Following code is just an Example.

以下代码只是一个示例。

Sub clear_lastWord_Variable()

    Dim wordArr() As String
    Dim lastword As String
    Do
        selection.Find.ClearFormatting
        selection.Find.Font.Bold = True
        With selection.Find
            .Forward = True
            .Wrap = wdFindStop
        End With
        selection.Find.Execute

        If selection.Find.Found Then

            wordArr = Split(selection, " ")

            For i = LBound(wordArr) To UBound(wordArr) Step 1
                lastword = wordArr(i)

            Next i

            MsgBox lastword

            ' here should be something to clear lastword
        Else
            Exit Do
        End If
    Loop

End Sub

回答by Andre

You can't "clear" a non-object variable, you can only set it to a defined value. For a string variable, that's usually the empty string "".

您不能“清除”非对象变量,只能将其设置为定义的值。对于字符串变量,通常是空字符串""

lastword = ""

or (identical)

或(相同)

lastword = vbNullString

For object variables, there is Set myObj = Nothing.

对于对象变量,有Set myObj = Nothing.

回答by I.Samp

If your variable is not an object you can also do:

如果您的变量不是对象,您还可以执行以下操作:

lastword = Empty