Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear / Empty Variable for next loop

I am having a situation in which I need to clear the variable lastWord so that msgbox show nothing in 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
like image 516
Ibn e Ashiq Avatar asked May 15 '16 07:05

Ibn e Ashiq


People also ask

How do you clear the value of a variable?

To delete a variable, along with its value, use Remove-Variable or Remove-Item . This cmdlet does not delete the values of variables that are set as constants or owned by the system, even if you use the Force parameter. If the variable that you are clearing does not exist, the cmdlet has no effect.

Does End Sub clear variables?

Remarks. When executed, the End statement resets all module-level variables and all static local variables in all modules. To preserve the value of these variables, use the Stop statement instead. You can then resume execution while preserving the value of those variables.

How do you clear a variable in Excel?

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 "" . For object variables, there is Set myObj = Nothing . Please look below, VARIANT type variable can be set to EMPTY and then checked by IsEmpty() function.

How do you clear a string variable?

C++ String clear() This function removes the elements, becomes an empty string.


2 Answers

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.

like image 154
Andre Avatar answered Oct 24 '22 10:10

Andre


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

lastword = Empty
like image 7
I.Samp Avatar answered Oct 24 '22 09:10

I.Samp