How to "kill" a class in VBA - Excel

Asked

Viewed 90 times

0

I have a code that calls a Private sub that in turn erases the data from a spreadsheet. If I do the same procedure without closing the form it gives error when deleting the data, but if I create a new sub with another name and the same code it runs normally twice. I wanted to know if there is a method like Dispose for objects in other languages, something that kills the sub after running everything inside it to be able to run again.

  • Good afternoon, I believe that trying to solve the problem that occurs would be the easiest and most correct way for your purpose. Put the code part of this sub so we can help you.

  • Sub is not a class module

1 answer

2


This is a code I just created for another user. Converting dates.

According to what you mentioned above, if I understand correctly you want to script while performing such a procedure, right? If positive try putting the command Exit Sub (no quotes) at the place where you want to stop the code. Follow template below.

Look for this remark in the script below to better understand: <----- este é o código que faz parar o script.

inserir a descrição da imagem aqui

Dim CData As Date
Dim CData2 As Date


Sub DataCompra1()
    {'--- FORMATANDO DATA MOD 01}

    [C8].Select
    CData = [C8].Value

    If CData = "0" Then
        MsgBox "INFORME A DATA"
        'Exit Sub '--- ESTA FUNÇÃO PARA O SCRIPT, CASO QUEIRA UTILIZAR DESCOMENTE A LINHA
    Else

        ActiveCell.Value = ""
        Selection.Offset(0, 2).Select
        ActiveCell.Value = CData
        Exit Sub                                 '<----- ESTE É O CÓDIGO QUE FAZ PARAR O SCRIPT.
        MsgBox CData

    End If

End Sub

Sub DataCompra2()
    {'--- FORMATANDO DATA MOD 02}

    Dim CData As Date
    Dim CHora As Date

    dt = [C8].Value
    CData = Format(dt)
    CHora = Format(Now)

    [E8].Value = CData
    [E9].Value = CHora

End Sub

Sub LimparData()

    [C8].Value = ""
    [E8].Value = ""

End Sub

Browser other questions tagged

You are not signed in. Login or sign up in order to post.