Consulting

Results 1 to 2 of 2

Thread: repeate an action

  1. #1
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location

    repeate an action

    hello
    i want to enable a user repeate an action-get another chance - if a module name he provided do not exists.
    [VBA]
    Sub countcod()
    On Error GoTo err
    Dim MyCodeMod
    Dim tryagain As Boolean
    Dim x As String
    tryagain = True
    Do While tryagain = True
    x = InputBox("how many lines of code in? ", "get a numer of line code")
    Set MyCodeMod = ActiveWorkbook.VBProject.VBComponents(x).CodeModule
    MsgBox " your module has " & MyCodeMod.CountOfLines & " lines of code", vbInformation, "number of lines in a vba module"
    tryagain = False
    Loop
    Exit Sub
    err:
    If MsgBox("module do not exist,please try again", vbCritical + vbOKCancel, "module is missing") = vbOK Then tryagain = False

    End Sub

    [/VBA]
    once the user have entered a wrong module name a msgbox will ast wheter he wans to try again.what is wrong with my code?
    thanks
    moshe

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This will loop until a code module's name is entered or Cancel is pressed.
    Sub countcod()
        Dim MyCodeMod As Object
        Dim x As String
        
        Set MyCodeMod = Nothing
        
        Do
            x = InputBox("how many lines of code in? ", "get a numer of line code")
            If StrPtr(x) = 0 Then Exit Sub: Rem Cancel pressed
            On Error Resume Next
                Set MyCodeMod = ActiveWorkbook.VBProject.VBComponents(x).CodeModule
            On Error GoTo 0
            If MyCodeMod Is Nothing Then
               MsgBox "module do not exist,please try again", Title:="module is missing"
            End If
        Loop While MyCodeMod Is Nothing
        
        MsgBox " your module has " & MyCodeMod.CountOfLines & "  lines of code", vbInformation, "number of lines in a vba module"
    
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •