PDA

View Full Version : disable an inputbox



lior03
08-09-2007, 10:16 AM
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?

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


thanks

lucas
08-09-2007, 10:20 AM
Your code does not add a sheet when you press cancel when I tried it.

lior03
08-09-2007, 10:25 AM
how can i generally disable an inputbox if the user do not enter a value.

lucas
08-09-2007, 10:31 AM
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.

mikerickson
08-09-2007, 12:04 PM
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

rory
08-10-2007, 05:56 AM
Moshe,
Here is a revised version of your code:
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

lior03
08-10-2007, 06:24 AM
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.

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


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

rory
08-10-2007, 06:28 AM
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 (http://www.microsoft.com/downloads/details.aspx?FamilyID=00535334-c8a6-452f-9aa0-d597d16580cc&displaylang=en)