Computing.Net > Forums > Office Software > Autochange Yes/No to Y/N in Excel

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Autochange Yes/No to Y/N in Excel

Reply to Message Icon

Name: Terri25
Date: March 18, 2009 at 13:13:54 Pacific
OS: Windows XP
Subcategory: Microsoft Office
Comment:

How can I set formatting in Excel columns so that when users type "Yes" or "No", the cell formats the entry as "Y" or "N"?



Sponsored Link
Ads by Google

Response Number 1
Name: Mike (by mmcconaghy)
Date: March 18, 2009 at 15:22:57 Pacific
Reply:

As far as I know, TEXT can not be Custom Formatted in Excel.

MIKE


0

Response Number 2
Name: DerbyDad03
Date: March 18, 2009 at 19:22:33 Pacific
Reply:

Right click on the sheet tab, choose View Code, and paste in this code:

Private Sub Worksheet_Change(ByVal Target As Range)
 If UCase(Target) = UCase("Yes") Then Target = "Y"
 If UCase(Target) = UCase("No") Then Target = "N"
End Sub

Every time a change is made to the worksheet, the code will check the input and if it's Yes or No, it'll change it to Y or N.

UCase is used so the code will handle Yes, YES, yes, YeS, etc. by internally setting the contents to all uppercase and then comparing it to an all uppercase Yes or No.


0

Response Number 3
Name: Mike (by mmcconaghy)
Date: March 18, 2009 at 19:40:27 Pacific
Reply:

Very nice, think I'll save this one myself. :-)

MIKE


0

Response Number 4
Name: Terri25
Date: March 19, 2009 at 05:48:54 Pacific
Reply:

That's beautiful and works wonders! Thank you DerbyDad03! And thank you, Mike, for a great site!


0

Response Number 5
Name: DerbyDad03
Date: March 19, 2009 at 05:56:45 Pacific
Reply:

Worksheet_Change and Selection_Change macros can be pretty useful.

There are lots of other Worksheet and Workbook based macros that can be used for all sorts of interesting things.


0

Related Posts

See More



Response Number 6
Name: Terri25
Date: March 19, 2009 at 06:07:14 Pacific
Reply:

Good to know! Is that the best way for me to set a column reserved for the 2-letter State to force uppercase in the whole column?


0

Response Number 7
Name: DerbyDad03
Date: March 19, 2009 at 06:49:40 Pacific
Reply:

I'm not sure what you're asking re: 2-letter stste column.

Please clarify


0

Response Number 8
Name: Terri25
Date: March 19, 2009 at 06:56:15 Pacific
Reply:

Sorry, I should have been more specific. In the same worksheet, Column E is where users will enter the 2-characters for states, i.e., AL, AZ MI, MN, NY, OK, PA, WI, etc. Some users are entering Al, Az, Mi, Mn, Ny, Ok, Pa, Wi, etc. I'd like to force the column to return all upper-case letters. Can I do this by simply adding another line to the macro code you provided above?


0

Response Number 9
Name: DerbyDad03
Date: March 19, 2009 at 08:12:55 Pacific
Reply:

Yes, you can do it with one line, but the line would be slightly different than the others.

The first 2 lines are not forcing anything in the Excel sheet to be uppercase. They are reading the contents of the cell and forcing it to be uppercase in "VBA memory" so that VBA can compare apples to apples regardless of what the user enters. If we didn't do it that way, we'd have to check for every combination of Yes, YeS, etc. line by line. The code then puts a "Y" or "N" in the cell based on the input.

Your second request is slightly different. You are asking for 2 things:

1 - Force the contents of the actual cell to be uppercase
2 - Limit this to Column E.

So first we have to check and see if the Target cell is in Column E, then, if it is, we can replace the contents with the uppercase version.

Note: VBA checks some things with a "double negative" i.e. if Not something is Nothing, then it must be something. That's why the new line might look a little strange.

Here's the code to handle Yes and No in every cell in the sheet and to force the contents of any cell in Column E to be uppercase:

Private Sub Worksheet_Change(ByVal Target As Range)
 If UCase(Target) = UCase("Yes") Then Target = "Y"
 If UCase(Target) = UCase("No") Then Target = "N"
 If Not Intersect(Target, Columns("E")) Is Nothing Then Target = UCase(Target)
End Sub


0

Response Number 10
Name: Terri25
Date: March 19, 2009 at 09:04:22 Pacific
Reply:

Much better! Since this will eventually be a template that people will use to create a new spreadsheet each month, I'm suspecting that some will just go in and highlight a bunch of cells and click the delete button. When I tried this, I received a Microsoft Visual Basic error message: Run-time error '13': Type mismatch, with the options to End, Debug or Help. Is there a way to turn off that error message so that users won't see it? Thanks so much for your patience with me!


0

Response Number 11
Name: DerbyDad03
Date: March 19, 2009 at 09:39:26 Pacific
Reply:

You might have other issues to deal with. If users don't have Macros enabled on their machine, they are going to get a warning when they try to open the workbook.

The warning they get will differ based on the level of Macro Security they have set.

This should deal with users who select more than one cell.

Private Sub Worksheet_Change(ByVal Target As Range)
 If Target.Cells.Count > 1 Then Exit Sub
 If UCase(Target) = UCase("Yes") Then Target = "Y"
 If UCase(Target) = UCase("No") Then Target = "N"
 If Not Intersect(Target, Columns("E")) Is Nothing Then Target = UCase(Target)
End Sub

I should note that it's next to impossible for me to anticipate how users will use a worksheet, so it's next to impossible for me to write the perfect code. It would behoove you to study the code and see what it is doing as a means to learn a little about VBA. If you are going to distribute workbooks with macros in them, then there should someone available who can maintian those macros. As a simple example, let's say you move the States to Column D. Someone will have to go into the macro and modify the code.



0

Response Number 12
Name: Terri25
Date: March 19, 2009 at 09:48:54 Pacific
Reply:

Excellent! You're a rock star! Thanks again so much for your help!


0

Response Number 13
Name: DerbyDad03
Date: March 19, 2009 at 09:53:02 Pacific
Reply:

re: You're a rock star!

You say that now...I'll just wait until the next issue pops up ;-)


0

Sponsored Link
Ads by Google
Reply to Message Icon

3 time installs Wordperfect Office 11



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Autochange Yes/No to Y/N in Excel

unable to right click in Excel 2007 www.computing.net/answers/office/unable-to-right-click-in-excel-2007/9142.html

URL Encode text in Excel 2007 www.computing.net/answers/office/url-encode-text-in-excel-2007/6746.html

Move to specific page in excel work www.computing.net/answers/office/move-to-specific-page-in-excel-work/4910.html