PDA

View Full Version : Solved: TextBox formula



Barryj
06-03-2008, 10:00 AM
I want put a formula in a TextBox2 that should read:
IF(TextBox1<R1C13Sheet1,"A","B")
is this on the right track, I have not been able to get it to work?

grichey
06-03-2008, 10:20 AM
I'm curious how you select a text box as well.

Barryj
06-03-2008, 10:31 AM
What is happening is that a user puts a number in textbox1 and if that number is less than the number on sheet 1 cell M1 then the letter A appears in textbox2 if others wise the letter B appears.

Hope this make a little sense?

Bob Phillips
06-03-2008, 10:35 AM
Textboxes are not worksheet objects, so you cannot test them in a formula, you have to use VBA.

RonMcK
06-03-2008, 10:38 AM
Load this in your UserForm module:
Private Sub TextBox1_Change()

Load UserForm1
If Val(TextBox1.Value) < Cells(1, 1) Then ' I used A1 (1,1) for my use; change to M1 (13,1) for yours.
TextBox2.Value = "A"
Else
TextBox2.Value = "B"
End If

End Sub
Something like this is what you need to use. Notice that you do not use the IF syntax of the spreadsheet but rather the Basic syntax (for VBA in this case). If you already ahve a TextBox1_Change() event, you'll need to add this to it.

Cheers,

Barryj
06-03-2008, 11:24 AM
At the moment it is only putting either the A or the B no matter what the number in textbox1.

Its not working in the fact that if the number is less than M1 it should be A and if greater should be B.

Tried mucking around with it but not able to get it to work, any thoughts?

Must be close Ron as it is putting a letter in the right spot.

RonMcK
06-03-2008, 11:38 AM
Barryj,

Hmm? Please help me here. What are you expecting that you are not seeing?

What value did you put in M1? What values when entered in Textbox1 yield an 'A' in Textbox2 and what values yield a 'B'? What did you get when Textbox1 = M1?

Thanks,

Barryj
06-03-2008, 11:47 AM
Say the number 10 is in cell M1 then when 9 is entered into textbox1 the letter A will be put in textbox2, if the number in textbox1 is greater than M1 then the letter B is put in textbox2, if nothing is in textbox1 then nothing to be shown in textbox2.

Hope this makes more sense.

Thanks for the help Ron.

RonMcK
06-03-2008, 11:51 AM
barryj,

So, when you enter each of those values in M1 and textbox1, what do you get in textbox2? I need to change the code for the blank M1 but before I do that please let me know what you are getting. In particular, what is happening wrong.

Thanks!

Barryj
06-03-2008, 11:55 AM
It is only showing the A at the moment if I swap the <> around then it only shows the B, its not showing the B if the textbox1 number is greater than number in M1.

RonMcK
06-03-2008, 12:14 PM
Barryj,

Give me a half hour or so, I have a meeting to attend.

RonMcK
06-03-2008, 12:42 PM
barryj,

Try this:Private Sub TextBox1_Change()

Dim testcell As Long
testcell = Cells(1, 13)
Debug.Print "before", Cells(1, 13).Value, ">" & TextBox1 & "<", ">" & TextBox2 & "<"
Load UserForm1 'Adjust User Form number as needed
' Change Cell(1, 13) reference if not using cell M1 for your test
If Str(testcell) = "" Then
TextBox2.Value = ""
Else
If Val(TextBox1) < testcell Then
TextBox2.Value = "A"
Else
TextBox2.Value = "B"
End If
End If
Debug.Print "after", Cells(1, 13).Value, ">" & TextBox1 & "<", ">" & TextBox2 & "<"
End Sub
Open the View > Immediate window and use F8 to step thru the code if you want. Remark out (or delete) the debug.print lines when this is working the way you want.

Thanks,

Barryj
06-03-2008, 04:19 PM
Thankyou very much Ron that is working fine, I will mark this thread as solved.

RonMcK
06-04-2008, 06:26 AM
Barryj,

You're very welcome. Glad to be able to assist you.

Cheers,