VBA in Excel - Userform Variable = textbox.text but only when textbox.text is not empty

StackOverflow https://stackoverflow.com/questions/23580177

  •  19-07-2023
  •  | 
  •  

Pregunta

I am having an issue with ensuring that Variable = textbox.text but only when textbox.text is not empty

I have a series of checkboxes on my userform each of them ties to a variable whose value is an integer based on labels caption. One of them needs to be tied to a textbox to allow for additional costs to be added to the total and needs to have an error to prompt user if the checkbox value is true but the textbox is empty, this I have done fine. The issue is that the variable for tied to the textbox text isnt working and my guess is that its linked to the error so i have to ensure the variable only = the textbox text if the textbox text is not empty.

This is my code thus far

    If chkbOther.Value = True And txtOther.Text < "" Then Other = CDbl(txtOther.Text)
        If chkbOther.Value = False Then Other = 0
    If chkbOther.Value = True And txtOther.Text = "" Then MsgBox "Please enter the additional service cost", vbCritical, "Missing Value Error"
¿Fue útil?

Solución

I think this might actually be a flow problem. I've rewritten this a bit and added the use of the IsNumeric and Val functions which are a bit more appropriate to what you're trying to achieve.

If chkbOther.Value = True Then
    If IsNumeric(txtOther.Text) = False Then
        MsgBox "Please enter the additional service cost", vbCritical, "Missing Value Error"
    Else
        Other = Val(txtOther.Text)
    End If
Else
    Other = 0
End If
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top