Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Inputbox and cancel button

Hi All,

I am rather confused as to why the following macro does not work properly. The
problem is when the user presses the cancel button without entering anything in
the Inputbox it starts looping! However, if the user presses OK without
entering anything in the Inputbox then it exits! Can you see why. Thanks for
your help.

Dim UInput as string
Dim Msg as string
Msg = "Enter todays date"

Do
UInput = Application.inputbox(Msg)
If UInput = "" then Exit Sub
If Isdate(UInput) then
Msgbox "Thank You"
Exit Do
End if
Msg = "You did not enter a date"
Msg = Msg & vbNewLine
Msg = Msg & "Enter todays date"
Loop

Regards
UJ
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Inputbox and cancel button

take a look at the Inputbox method in VBA Help. If the user click
Cancel, the value False is returned, which the assignment operator
coerces to a string.


Try this instead:

Const Msg1 As String = "Enter today's date"
Const Msg2 As String = "You did not enter a valid date." & _
vbNewLine & "Please enter today's date"
Dim UInput As Variant
Dim bLoop As Boolean

Do
UInput = Application.InputBox( _
Prompt:=IIf(bLoop, Msg2, Msg1), _
Default:=Date)
If UInput = False Then Exit Sub
bLoop = True
Loop Until IsDate(UInput)

n article ,
(Uddinj1) wrote:

Hi All,

I am rather confused as to why the following macro does not work properly.
The
problem is when the user presses the cancel button without entering anything
in
the Inputbox it starts looping! However, if the user presses OK without
entering anything in the Inputbox then it exits! Can you see why. Thanks for
your help.

Dim UInput as string
Dim Msg as string
Msg = "Enter todays date"

Do
UInput = Application.inputbox(Msg)
If UInput = "" then Exit Sub
If Isdate(UInput) then
Msgbox "Thank You"
Exit Do
End if
Msg = "You did not enter a date"
Msg = Msg & vbNewLine
Msg = Msg & "Enter todays date"
Loop

Regards
UJ

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Inputbox and cancel button

When the cancel button is pressed uinput = false

You code only checks for ""

This is why your code is not exiting sub

modify your code

If UInput = "" or Uinput = false then Exit Su

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Inputbox and cancel button

The cancel button is returning a value of False
Change hte IF statement to
IF UInput= False then Exit Sub

That should work

Andres

-----Original Message-----
Hi All,

I am rather confused as to why the following macro does

not work properly. The
problem is when the user presses the cancel button

without entering anything in
the Inputbox it starts looping! However, if the user

presses OK without
entering anything in the Inputbox then it exits! Can you

see why. Thanks for
your help.

Dim UInput as string
Dim Msg as string
Msg = "Enter todays date"

Do
UInput = Application.inputbox(Msg)
If UInput = "" then Exit Sub
If Isdate(UInput) then
Msgbox "Thank You"
Exit Do
End if
Msg = "You did not enter a date"
Msg = Msg & vbNewLine
Msg = Msg & "Enter todays date"
Loop

Regards
UJ
.

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Inputbox and cancel button

on reflection, I'm confused about why this inputbox is even necessary.
Today's date can always be found using the Date function:

UInput = Date

In article ,
(Uddinj1) wrote:

I am rather confused as to why the following macro does not work properly.



  #6   Report Post  
Posted to microsoft.public.excel.programming
UJ UJ is offline
external usenet poster
 
Posts: 1
Default Inputbox and cancel button

Hi,
I did not get to see your reply until now. I used today's
date as an example. I need a date entered depending on
other issues.

However, the answer you provided works and it is
exteremely useful to me. Thank you. Best wishes.

Regards

UJ
-----Original Message-----
on reflection, I'm confused about why this inputbox is

even necessary.
Today's date can always be found using the Date function:

UInput = Date

In article ,
(Uddinj1) wrote:

I am rather confused as to why the following macro does

not work properly.
.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting Cancel in an InputBox Method Connie Excel Discussion (Misc queries) 2 October 19th 06 01:32 PM
Cancel button in Inputbox method MiRa Excel Programming 2 November 14th 03 01:04 PM
Inputbox method using type:=8 - How to Cancel? Joe 90 Excel Programming 0 July 10th 03 12:24 AM
Inputbox method using type:=8 - How to Cancel? Harlan Grove[_5_] Excel Programming 1 July 9th 03 12:06 AM
Inputbox method using type:=8 - How to Cancel? Jim Cone Excel Programming 0 July 8th 03 06:15 PM


All times are GMT +1. The time now is 01:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"