Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

How to use an input mask in excel vba?

READ CAREFULLY THE QUESTION PLEASE, IT MENTIONS EXCEL VBA. NOT EXCEL

I was given the task of fixing a date input on a VBA form. A textbox should have the user enter the date as MM/DD/YYYY.

I am required to use an input mask, not allowed to do something as validating date after or using a calendar. So far I was able to use the 2 methods mentioned (forcing the format after using ISDATE).

However, it has now been made clear it has to be a mask so keys are filtered on entry, with the mask being visible when entering the date: __/__/____

Where you see underscore, he should only be able to enter numbers and the / are always at those positions

Is there a way to do this? I can only find a tutorial for the mask in Access VBA.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I Googled "Excel input mask" and the 2nd result was: Using an Input Mask Microsoft Excel written by Allen Wyatt...

Using an Input Mask

...You may wonder if there is a way to set up an input mask that will add the colon automatically. The good news is yes, there is. The bad news is no, there isn't. Sound confusing? Let me explain...

You can set up a custom format that will display your time in any format you want. For instance, you could use the following steps:

  1. Select the cells you want to use for time input.
  2. Choose Format from the Cells menu. Excel displays the Format Cells dialog box.
  3. Make sure the Number tab is displayed.
    image: The Number tab of the Format Cells dialog box.
  4. In the Category list, choose Custom.
  5. Replace whatever is in the Type box with #":"00.
  6. Click on OK.

You can now enter your times using just digits. The problem (and this is the bad news) is that the cell doesn't really contain a time. If you enter 230 (for 2:30), it doesn't contain 2:30 as a time—it contains two hundred and thirty. Thus, you can't use the contents of the cell directly in time calculations.

To overcome this, you can use another column to show the entered digits converted into a time. All you need to do is use a formula to do the conversions. For instance, if the time you entered was in cell A3, you could use the following formula in a different cell to do the conversion:

=(INT(A3/100)/24)+((A3 - (INT(A3/100)*100))/1440)

Format the cell that contains the above formula so it displays one of the various time formats, and you are all set.

(Full article and more at the source.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...