+ Reply to Thread
Results 1 to 7 of 7

Trying to understand IF Else Statements

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Trying to understand IF Else Statements

    Hello,

    Can someone take a look at some basic code I wrote and help me figure out what i'm doing wrong.

    I would like for a person to enter 2 colors (red, blue or yellow) to make a new color.

    If they choose any other color they get an error.

    Sub colorMix()
    
    Dim color1 As String
    Dim color2 As String
    
    color1 = InputBox("Enter the first color")
    color2 = InputBox("Enter the second color")
    
    If Not color1 = "Red" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Not color1 = "Blue" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Not color1 = "Yellow" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Not color2 = "Red" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Not color2 = "Blue" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Not color2 = "Yellow" Then
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    
    If Color = "Red" And color2 = "Blue" Then
        MsgBox ("The new color is Purple")
    
    Else
    
    If color1 = "Red" And color2 = "Yellow" Then
       MsgBox ("The new color is orange")
    
    Else
    
    If color1 = "Blue" And color2 = "Yellow" Then
       MsgBox ("The new color is Green")
    
    End If
    End If
    End If
    End Sub
    Thank you very much
    Last edited by Justair07; 10-09-2013 at 08:20 PM.

  2. #2
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: Trying to understand IF Else Statements

    This should work. However, did you want a result if they select the same color in both input boxes?

    
    Dim color1 As String
    Dim color2 As String
    Dim color1flag As Boolean
    Dim color2flag As Boolean
    Dim differentColor As Boolean
    
    differentColor = false
    color1flag = false
    color2flag = false
    
    Do Until differentColor = True
    
    Do Until color1flag = True
    color1 = InputBox("Enter the first color")
    If color1 = "Red" Or color1 = "Blue" Or color1 = "Yellow" Then
        color1flag = True
    Else
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    Loop
    
    Do Until color2flag = True
    color2 = InputBox("Enter the second color")
    If color2 = "Red" Or color2 = "Blue" Or color2 = "Yellow" Then
        color2flag = True
    Else
        MsgBox ("You can only enter Red, Blue, or Yellow")
    End If
    Loop
    
    If color1 = color2 Then
    differentColor = True
    Else
    MsgBox ("You selected the same color.  Please enter the colors again.")
    End If
    
    Loop
    If Color1 = "Red" And color2 = "Blue" Then
        MsgBox ("The new color is Purple")
    Else
    If color1 = "Red" And color2 = "Yellow" Then
       MsgBox ("The new color is orange")
    Else
    If color1 = "Blue" And color2 = "Yellow" Then
       MsgBox ("The new color is Green")
    End If
    End If
    End If
    Last edited by majosum; 10-09-2013 at 08:45 PM.

  3. #3
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Re: Trying to understand IF Else Statements

    Good question, same color should probably be a separate error message.

  4. #4
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: Trying to understand IF Else Statements

    I edited my code in my original post. Try that now.

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Trying to understand IF Else Statements

    Hi Justair07,
    Here's a code to try :

    Option Explicit
    Option Compare Text
    
    Sub ColorMix()
    
       Dim color1 As String
       Dim color2 As String
       Dim arColor1 As String
       Dim arColor2 As String
       Dim ColorMix As String
    
       arColor1 = "(Red, Blue, Yellow)"      'acceptable color1
       arColor2 = "(Red, Blue, Yellow)"      'acceptable color2
    
       color1 = InputBox("Enter the first color" & vbLf & arColor1, "Color 1")
       color2 = InputBox("Enter the second color" & vbLf & arColor2, "Color 2")
    
       Do Until InStr(1, arColor1, color1) > 0
          color1 = InputBox("You can only enter Red, Blue, or Yellow", "Color 1")
       Loop
    
       Do Until InStr(1, arColor2, color2) > 0
          color2 = InputBox("You can only enter Red, Blue, or Yellow", "Color 2")
       Loop
    
       Select Case color1
       Case "Red"
          Select Case color2
          Case "Red": ColorMix = "Red"
          Case "Blue": ColorMix = "Purple"
          Case "Yellow": ColorMix = "Orange"
          End Select
       Case "Blue"
          Select Case color2
          Case "Red": ColorMix = "Purple"
          Case "Blue": ColorMix = "Blue"
          Case "Yellow": ColorMix = "Green"
          End Select
       Case "Yellow"
          Select Case color2
          Case "Red": ColorMix = "Orange"
          Case "Blue": ColorMix = "Green"
          Case "Yellow": ColorMix = "Yellow"
          End Select
       End Select
       
       MsgBox ColorMix
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  6. #6
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Trying to understand IF Else Statements

    Just saw the condition to have 2 different colors...
    code slightly modified :

    Option Explicit
    Option Compare Text
    
    Sub ColorMix()
    
       Dim color1 As String
       Dim color2 As String
       Dim arColor1 As String
       Dim arColor2 As String
       Dim ColorMix As String
    
       arColor1 = "(Red, Blue, Yellow)"      'acceptable color1
       arColor2 = "(Red, Blue, Yellow)"      'acceptable color2
    
       color1 = InputBox("Enter the first color" & vbLf & arColor1, "Color 1")
       color2 = InputBox("Enter the second color" & vbLf & "Different from Color 1" & vbLf & arColor2, "Color 2")
    
       Do Until InStr(1, arColor1, color1) > 0
          color1 = InputBox("You can only enter Red, Blue, or Yellow", "Color 1")
       Loop
    
       Do Until InStr(1, arColor2, color2) > 0 And color1 <> color2
          color2 = InputBox("You can only enter Red, Blue, or Yellow" & vbLf & "Different from color1", "Color 2")
       Loop
    
       Select Case color1
       Case "Red"
          Select Case color2
          Case "Red": ColorMix = "Red"
          Case "Blue": ColorMix = "Purple"
          Case "Yellow": ColorMix = "Orange"
          End Select
       Case "Blue"
          Select Case color2
          Case "Red": ColorMix = "Purple"
          Case "Blue": ColorMix = "Blue"
          Case "Yellow": ColorMix = "Green"
          End Select
       Case "Yellow"
          Select Case color2
          Case "Red": ColorMix = "Orange"
          Case "Blue": ColorMix = "Green"
          Case "Yellow": ColorMix = "Yellow"
          End Select
       End Select
       
       MsgBox ColorMix
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    01-08-2013
    Location
    Jacksonville, Fl
    MS-Off Ver
    Excel 2016
    Posts
    355

    Re: Trying to understand IF Else Statements

    Thank you guys very much for the help! majosum, I got an error when I entered Red for the first color and blue for the 2nd. It said I entered the same color? GC Excel thank you very much for your help, your code worked great, I was hoping to add an error message instead of a repeated question. I'm going to play around with what you gave me so far. Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. what does 6.9544E+23 stand for?
    By gokarterid in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-30-2012, 11:50 AM
  2. Let the gurus stand up please
    By abambam in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2011, 12:54 PM
  3. Stand Alone program
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-18-2007, 10:47 AM
  4. What does the ! stand for in formulas?
    By ! in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-14-2005, 05:06 PM
  5. [SOLVED] Stand alone
    By Khalil Handal in forum Excel General
    Replies: 1
    Last Post: 03-08-2005, 07:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1