Consulting

Results 1 to 8 of 8

Thread: disable an inputbox

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

    disable an inputbox

    hello
    i want to enable auser name a sheet he adds.he can decide not to.
    suppose he wants to and an inputbox opens,if he changes his mind and press cancel the sheet adds anyway.this is not what i intended.
    if he press select i want the macro to end.how caould it be doen?
    [VBA]
    Sub AddWorksheet6()
    On Error Resume Next
    Dim response As VbMsgBoxResult
    Dim sht As Worksheet
    response = MsgBox("Do you want to name the sheet ?" _
    , vbQuestion + vbYesNoCancel, "add a new w.s")
    Select Case response
    Case vbCancel
    Exit Sub
    Case vbNo
    Worksheets.Add
    Case vbYes
    Worksheets.Add().name = InputBox("enter sheet name", "sheet name", "name goes here")
    End Select
    MsgBox " new sheet added as requested - " & sht.name, vbInformation, "new sheet added" & " - " & Format(Now, "dd/mm/yy hh:mm:ss")
    End Sub

    [/VBA]
    thanks
    moshe

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Your code does not add a sheet when you press cancel when I tried it.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    how can i generally disable an inputbox if the user do not enter a value.
    moshe

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Do you mean if they click NO or if they click Cancel?
    With your code when they click cancel it exits the sub and no sheet is added.
    If they click NO then a sheet is added as you have instructed it to do....you could give it a default name using an if statement...

    I'm sorry but I really do not understand the question.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
     
    Dim useResponce As String, newSheet As Worksheet
    useResponce = Application.InputBox("New sheet name, please", Type:=2)
    If useResponce = "False" Then
        MsgBox "Cancel pressed"
    Else
        Rem Add a sheet
        Set Worksheet = Worksheets.Add
        If useResponce <> vbNullString Then newSheet.Name = useResponce
    End If

  6. #6
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Moshe,
    Here is a revised version of your code:
    [VBA]Sub AddWorksheet6()
    On Error Resume Next
    Dim response
    Dim sht As Worksheet
    response = MsgBox("Do you want to name the sheet ?" _
    , vbQuestion + vbYesNoCancel, "add a new w.s")
    Select Case response
    Case vbCancel
    Exit Sub
    Case vbNo
    Set sht = Worksheets.Add
    Case vbYes
    response = InputBox("enter sheet name", "sheet name", "name goes here")
    If Len(response) = 0 Then Exit Sub
    Set sht = Worksheets.Add
    sht.Name = response
    End Select
    MsgBox " new sheet added as requested - " & sht.Name, vbInformation, _
    "new sheet added" & " - " & Format(Now, "dd/mm/yy hh:mm:ss")
    End Sub
    [/VBA]
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    VBAX Mentor
    Joined
    Jun 2005
    Posts
    374
    Location
    hello all
    i am working on a modified version.i added a word document with all instructions regarding this macro.i want to attach this as a help file.
    [VBA]
    Sub AddWorksheet55()
    Dim helpfile
    On Error GoTo canceled
    Dim response As VbMsgBoxResult
    Dim sht As Worksheet
    response = MsgBox("Do you want to name the sheet ?", vbQuestion + vbYesNo + vbMsgBoxHelpButton, "add a new w.s", ThisWorkbook.Path & "\mouse.doc")
    If response = vbYes Then
    Set sht = Worksheets.Add
    sht.name = InputBox("enter sheet name.", "please select a name:")
    If Len(response) = 0 Then Exit Sub
    ElseIf response = vbNo Then Exit Sub
    Else: Exit Sub
    End If
    MsgBox " new sheet added as requested - " & sht.name, vbInformation, "new sheet added" & " - " & Format(Now, "dd/mm/yy hh:mm:ss")
    canceled:
    End Sub

    [/VBA]
    this macro do not work? .why?
    how can i add a help file witha hlp extention? should i download some program?
    thanks
    moshe

  8. #8
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    You need something like the HTML Help workshop. Unless you have a lot more stuff to put in it, it would be a lot of work for something as simple as this code!

    PS You can download it from here
    Last edited by rory; 08-10-2007 at 06:42 AM.
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

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