If statement to set Textbox based on another Textbox

paublojobino

New Member
Joined
May 27, 2022
Messages
10
Office Version
  1. 2010
Platform
  1. Windows
Hi there,

Im wanting to set textbox "missionstart" to a value based on the value entered in textbox "flightstarthh"
I have tried this code below but whatever i do it always returns the value in the first if statement.

Where am i going wrong?

If flight1starthh.Value = "0" Or "1" Then
missionstart.Value = "0000 - 0159"
elseIf flight1starthh.Value = "2" Or "3" Then
missionstart.Value = "0200 - 0359"
ElseIf flight1starthh.Value = 4 Or 5 Then
'missionstart.Value = "0400 - 0559"
'ElseIf flight1starthh.Value = 6 Or 7 Then
'missionstart.Value = "0600 - 0759"
'ElseIf flight1starthh.Value = 8 Or 9 Then
'missionstart.Value = "0800 - 0959"
'ElseIf flight1starthh.Value = 10 Or 11 Then
'missionstart.Value = "1000 - 1159"
'ElseIf flight1starthh.Value = 12 Or 13 Then
'missionstart.Value = "1200 - 1359"
'ElseIf flight1starthh.Value = 14 Or 15 Then
'missionstart.Value = "1400 - 1559"
'ElseIf flight1starthh.Value = 16 Or 17 Then
'missionstart.Value = "1600 - 1759"
'ElseIf flight1starthh.Value = 18 Or 19 Then
'missionstart.Value = "1800 - 1959"
'ElseIf flight1starthh.Value = 20 Or 21 Then
'missionstart.Value = "2000 - 2159"
Else: missionstart.Value = "2200 - 2359"
end if
end sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
If flight1starthh.Value = "0" Or "1" Then

This is the wrong syntax. All of your If statements need to look like this:

VBA Code:
If flight1starthh.Value = "0" Or flight1starthh.Value = "1" Then

Also from a user interface design perspective, if you have a textbox that can only have 1-21 then you should use a combobox with those numbers instead of letting the user type in whatever comes into their head. What if they type FOOBAR?
 
Upvote 0
If flight1starthh.Value = "0" Or "1" Then

This is the wrong syntax. All of your If statements need to look like this:

VBA Code:
If flight1starthh.Value = "0" Or flight1starthh.Value = "1" Then

Also from a user interface design perspective, if you have a textbox that can only have 1-21 then you should use a combobox with those numbers instead of letting the user type in whatever comes into their head. What if they type FOOBAR?

thanks for your advice, i changed the syntax and still ALWAYS get the result from the first if statement. any ideas?
 
Upvote 0
Instead of If-ElseIf, why not use Select Case?

VBA Code:
    Select Case flight1starthh.Value
    Case "0", "1"
        missionstart.Value = "0000 - 0159"
    Case "2", "3"
        missionstart.Value = "0200 - 0359"
    Case "4", "5"
        missionstart.Value = "0400 - 0559"
    Case "6", "7"
        missionstart.Value = "0600 - 0759"
    Case "8", "9"
        missionstart.Value = "0800 - 0959"
    Case "10", "11"
        missionstart.Value = "1000 - 1159"
    Case "12", "13"
        missionstart.Value = "1200 - 1359"
    Case "14", "15"
        missionstart.Value = "1400 - 1559"
    Case "16", "17"
        missionstart.Value = "1600 - 1759"
    Case "18", "19"
        missionstart.Value = "1800 - 1959"
    Case "20", "21"
        missionstart.Value = "2000 - 2159"
    Case Else
        missionstart.Value = "2200 - 2359"
    End Select
 
Upvote 0
thanks for your advice, i changed the syntax and still ALWAYS get the result from the first if statement. any ideas?
@rlv01 has a good solution. I would still be interested in seeing your revised code so I can tell you why it doesn't work. Might be educational for both of us.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top