Search code examples
vbaexceltextboxis-empty

VBA Change value in textbox based on whether other textboxes are empty


Struggling with some code here, looking for help. I am trying to figure out the correct syntax for updating a textbox value based on whether another textbox has a value or not.

Breakdown

  • Textbox1 name = arisk_box
  • Textbox2 name = adjust_box3
  • Textbox3 name = rr_box

This is what I have so far:

Private Sub arisk_box_Change()
If arisk_box.Text <> "" And adjust_box3.Text = "" Then
    rr_box.Value = arisk_box.Value
 ElseIf arisk_box.Text <> "" And adjust_box3.Text <> "" Then
    rr_box.Value = adjust_box3.Value
 End If
 End Sub

   *If arisk_box has a *VALUE* and adjust_box3 *DOES NOT* then rr_box = the value from arisk_box
  Elseif arisk_box AND adjust_box3 both have a *VALUE* then rr_box = the value from adjust_box3

Solution

  • The simplest way it to use 2 separate macros, one for each textbox change event. But it may not work as smoothly as you want it to. I sure someone will have a more advance method.

    Private Sub adjust_box3_Change()
        If arisk_box <> "" And adjust_box3 = "" Then rr_box = arisk_box
    End Sub
    
    Private Sub arisk_box_Change()
        If arisk_box <> "" And adjust_box3 <> "" Then rr_box = adjust_box3
    End Sub