If/Then statements with three outcomes

Garrek

New Member
Joined
Aug 22, 2019
Messages
49
Hey all, I have a Userform with textboxes in it. A user can input up to two characters in each. How the values are formatted is dependent on how many characters there are i.e. Empty Textboxes are handled one way, single characters ("A") are handled another and 2 letters ("AB") are handled a separate way. Is there any easy way to sort these?

Right now I have the following code
Code:
If TB.Value = "" Then
Set xxxxxxxxxx
Else
Set xxxxxxx
End If
This takes care of empty text boxes and non empty ones, but I'm hoping to find a way to incorporate single vs 2 initial.
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Garrek

New Member
Joined
Aug 22, 2019
Messages
49
Sure, use a Case statement, i.e.
Code:
Select Case Len(TB.Value)
    Case 0
        Set XXX
    Case 1
        Set YYY
    Case 2
        Set ZZZ
    Case Else
        MsgBox "Error!"
End Select
See here for more information: https://www.techonthenet.com/excel/formulas/case.php

Is there a good way to quickly identify how many characters are in the textbox? I used TB.Value = "" to identify empty textboxes, but obviously that won't work for trying to differentiate between 1 and 2 character inputs.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,646
Office Version
365
Platform
Windows
Is there a good way to quickly identify how many characters are in the textbox?
Yes, and I already did it in my previous reply:
Code:
Select Case [COLOR=#0000ff]Len(TB.Value)[/COLOR]
The LEN function checks the length (number of characters) of the value entry in your "TB" textbox.

Try it out for yourself and see!
 
Last edited:

Garrek

New Member
Joined
Aug 22, 2019
Messages
49
Yes, and I already did it in my previous reply:
Code:
Select Case [COLOR=#0000ff]Len(TB.Value)[/COLOR]
The LEN function checks the length (number of characters) of the value entry in your "TB" textbox.

Try it out for yourself and see!

Missed that. Thank you! Working great so far.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
53,646
Office Version
365
Platform
Windows
You are welcome.
 

Watch MrExcel Video

Forum statistics

Threads
1,102,264
Messages
5,485,754
Members
407,512
Latest member
PearceK

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top