If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > If in VBA Excel

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-17-11, 04:25
math123 math123 is offline
Registered User
 
Join Date: Apr 2011
Posts: 8
If in VBA Excel

Hi

I have a macro which runs sql code. For the macro I am passing date value from Excel cell A1. The user need to enter the date manually.
Now I need to restrict the user in entering the value in the cell A1 in the format mm/dd/yyyy .
How include this condition in existing macro(VBA) code, so that when user enters date in someother format, a message box need to pop-up indicating to enter in the proper format?

Welcome for all the suggestions...
Reply With Quote
  #2 (permalink)  
Old 05-17-11, 05:28
myle myle is offline
(Making Your Life Easy)
 
Join Date: Feb 2004
Location: New Zealand
Posts: 1,143
isdate() will return a true/false

so

if not isdate(range("A1")) then Msgbox("NOT A DATE VALUE")


Just learnt this tick write some code that will run vb on a cell change


so in the vb part of that worksheet (not a Module) took me an 1/2 to work this out didn't read the help right LOL

Private Sub Worksheet_Change(ByVal Target As Range)

Dim checkdate as Boolean

select case Target.address

case "$A$1" ' a1 was change
checkdate = isdate(Target.Value) if its not a date value it will be false
if not checkdate then
Msgbox("this is not a date value")
else
call sub_go_do_something(Target.Value)
End if

case else
end select

End sub

now this can be in a module

sub_go_do_something(thisvalue)

msgbox("You inputed " & format(thisvalue,"mm/dd/yy") & " Date")


End sub
__________________
hope this help

See clear as mud


StePhan McKillen
the aim is store once, not store multiple times
Remember... Optimize 'til you die!
Progaming environment:
Access based on my own environment: DAO3.6/A97/A2000/A2003
VB based on my own environment: vb6 sp5
ASP based on my own environment: 5.6
VB-NET based on my own environment started 2007
SQL-2005 based on my own environment started 2008
MYLE

Last edited by myle; 05-17-11 at 05:47. Reason: spelling
Reply With Quote
  #3 (permalink)  
Old 05-17-11, 12:20
math123 math123 is offline
Registered User
 
Join Date: Apr 2011
Posts: 8
VBA Excel

Hey, thanks a lot.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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