VBA TextBox IF Then Else

rameezl17

Board Regular
Joined
Mar 6, 2018
Messages
105
Hi All,

I have been trying to figure this out for the past hour and I don't understand what I am doing wrong. I have a userform with a textbox. This textbox is grabbing a value from an excel spreadsheet only when a cell in an excel sheet says yes. Otherwise if the cell says no I need the user to input a number. I have this code in now. The problem is that when the cell says "No", and I type into the textbox, the textbox automatically deletes what i type in...Im assuming this is because of the "". Any help would be greatly appreciated, thank you!

Private Sub TextBox1_AfterUpdate()
If Worksheets("Sec. 8").Range("J15").Value = "Yes" And Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
TextBox1.Value = Worksheets("Sec. 8").Range("F17")
ElseIf Worksheets("Sec. 8").Range("J15").Value = "Yes" Then
TextBox1.Value = Worksheets("Sec. 8").Range("F15")
ElseIf Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
TextBox1.Value = Worksheets("Sec. 8").Range("F16")
ElseIf Worksheets("Sec. 8").Range("J15").Value = "No" Then
TextBox1.Value = ""
ElseIf Worksheets("Sec. 8").Range("J16").Value = "No" Then
TextBox1.Value = ""
End If
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You need to put that code in the Intialize event. Unless the userform changes the values of J15 or J16, you want to set the value of the textbox at the start and then disable user entry when appropriate

Code:
Private Sub Userform_Initialize()

    TextBox1.Enabled = False
    If Worksheets("Sec. 8").Range("J15").Value = "Yes" And Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F17")
    ElseIf Worksheets("Sec. 8").Range("J15").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F15")
    ElseIf Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F16")
    Else
        TextBox1.Enabled = True
        TextBox1.Value = ""
    End If
End Sub
 
Upvote 0
You need to put that code in the Intialize event. Unless the userform changes the values of J15 or J16, you want to set the value of the textbox at the start and then disable user entry when appropriate

Code:
[table="width: 500"]
[tr]
	[td]Private Sub Userform_Initialize()
    TextBox1.Enabled = False
    If Worksheets("Sec. 8").Range("J15").Value = "Yes" And Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F17")
    ElseIf Worksheets("Sec. 8").Range("J15").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F15")
    ElseIf Worksheets("Sec. 8").Range("J16").Value = "Yes" Then
        TextBox1.Value = Worksheets("Sec. 8").Range("F16")
    Else
        TextBox1.Enabled = True
        TextBox1.Value = ""
    End If
End Sub[/td]
[/tr]
[/table]
In addition to Mike's comment, you find it interesting that the above code can be condensed somewhat... the following code will do the same thing as the above code does.
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Userform_Initialize()
  Dim Ofst As Long
  Ofst = ['Sec. 8'!J15="Yes"] + 2 * ['Sec. 8'!J16="Yes"]
  Sheets("Sec. 8").TextBox1.Enabled = Not CBool(Ofst)
  Sheets("Sec. 8").TextBox1.Value = Evaluate("IF(" & Ofst & ",OFFSET('Sec. 8'!F14," & -Ofst & ",0),"""")")
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,639
Messages
6,173,496
Members
452,516
Latest member
druck21

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