PDA

View Full Version : repeate an action



lior03
12-11-2007, 12:29 AM
hello
i want to enable a user repeate an action-get another chance - if a module name he provided do not exists.

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


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

mikerickson
12-11-2007, 12:59 AM
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