LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #12   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default ccard 6 or 16 digits

you could change the else statement to test the length for 6 or 16 if you
want to know the length of what they input.

If (Len(Target.Value) < 6) And (Len(Target.Value) < 16) Then _
MsgBox "Invalid Length"

Encapsulate the entire pattern matching test with the first If statement as
follows and you won't get the message box for every change event.

If Not Intersect([Ccard], Target) Is Nothing Then
If [Ccard].Formula Like "######" Then
[Ccard].Value = Format([Ccard].Value, "##xx xxxx xxxx ####")
ElseIf [Ccard].Formula Like "################" Then
[Ccard].Value = Format([Ccard].Value, "#### #### #### ####")
Else: Msgbox "Invalid Data. Please enter a 6 or 16 digit number."
End If
End If

As long as you preformat the cell to text (Format/Cells/Text), you wouldn't
have to change the numberformat to text via code. You could not rely on the
Change event to set the format to text as it would be too late.


"stefan via OfficeKB.com" wrote:


Hi JMB,
I like that. I applied this to my worksheet_change vs. the
Worksheet_SelectionChange event.
i could not figure out how to incorporate it into the
Worksheet_SelectionChange and function properly. (all my columns of the
worksheet expanded to full screen or larger - strange.) when i add it as
shown below to worksheet change it works, as far as i can tell, without any
problems. i am happy with that. thank you for the help.

i was wondering if there might be an option to add a msg - if the value
entered in the range 'ccard' is less then 6 digits, more then 16 or between 6
and 16 digits. when i leave the code
Else
MsgBox "Invalid Number"

in, it prompts at any worksheet_change (obviously).

Thank you
Stefan

....
If Not Intersect([Ccard], Target) Is Nothing Then _
Target.NumberFormat = "@"
If [Ccard].Formula Like "######" Then
[Ccard].Value = Format([Ccard].Value, "##xx xxxx xxxx ####")
ElseIf [Ccard].Formula Like "################" Then
[Ccard].Value = Format([Ccard].Value, "#### #### #### ####")
End If
....



JMB wrote:
I think I see what you mean. The cell will need to be formatted as text
before you key anything in as Excel will convert it right away. You could
ensure this by pasting this into your worksheet module (right click on sheet
tab and paste into the module)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect([Ccard], Target) Is Nothing Then _
Target.NumberFormat = "@"
End Sub

Then try some small changes to Pauls macro:

Sub FormatCcard()
If [Ccard].Formula Like "######" Then
[Ccard].Value = Format([Ccard].Value, "##xx xxxx xxxx ####")
ElseIf [Ccard].Formula Like "################" Then
[Ccard].Value = Format([Ccard].Value, "#### #### #### ####")
Else
MsgBox "Invalid Number"
End If

End Sub

Hi JMB,
Thanks for the suggestion.

[quoted text clipped - 12 lines]

Paul D



--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200507/1

 
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
How do you change single digits to recognized double digits? Evil with a K[_2_] Excel Worksheet Functions 5 April 3rd 23 02:29 PM
VBA write macro change column with 3 number digits to 4 digits the James C[_2_] Excel Discussion (Misc queries) 3 January 25th 10 03:12 PM
number 12 digits to 15 digits mehdy-e Excel Discussion (Misc queries) 5 November 13th 09 04:43 PM
How to customize number to 10 digits including 2 digits after deci Carina Excel Worksheet Functions 3 September 20th 07 02:50 AM
last ten digits Martina Excel Worksheet Functions 5 December 1st 06 04:53 AM


All times are GMT +1. The time now is 09:29 PM.

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

About Us

"It's about Microsoft Excel"