Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Userform textbox update

0

[G'day to you @ teach excel; Recently, I've been working on a db for a home improvements project I have lined up. I decided to do a small db as an inventory for materials required. Unfortunately, I got bogged down with some VBA which I need some assistance please.

I have used this Userform db many times before with no hassle at all over and over again, but I wanted a little small change.

 I wanted to have one of the textbox, txtBx4 to populate from a code from the previous textbox, txtBx3.

 I E: if it is "Al" it should populate the textbox with "Aluminium", if it's" St" it would be "Steel".

Please, I would appreciate if I can get some assistance on this little code.

I've tried this with 'AfterUpdate'. Similar code worked for me in another db but unfortunately for some reason on this one it wouldn't work done for it on this particular db is not working for me.

I'm using Excel 2024 LTSC. I will upload a sample of the db as well.

Many Thanks

Joe 

Answer
Discuss

Answers

0
Selected Answer

Hi Joe..

In the attached file, I firstly modified your code for Private Sub txtBx4_AfterUpdate()  behind the Testdb UserFormto:

  1. act when Enter is pressed after text is added to txtBx3 (under "Code" in the form)
  2. be a series of If/ Then statements as leblow:
Private Sub txtBx3_AfterUpdate()
    'Updated to change txtBx4 based txtBx3 text

    If Me.txtBx3.Value = "" Then
    Me.txtBx4 = ""
    End If
    If Me.txtBx3 = "Al" Then
    Me.txtBx4 = "Aluminium"
    End If
    If Me.txtBx3 = "St" Then
    Me.txtBx4 = "Steel"

    End If
End Sub

While that works, I've commented that out in favour of the Select Case approach - which detects incorrect codes.(see the "Case Else" lines after the blank line "Case Else"):and would allow you to easily expand it for other Code categories e.g."Tu" for Tungsten Steel"):

Private Sub txtBx3_AfterUpdate()
    ' Alternative code using Cases (easily expanable)
    Select Case Me.txtBx3.Value
        Case ""
        Me.txtBx4 = ""
        Case "Al"
        Me.txtBx4 = "Aluminium"
        Case "St"
        Me.txtBx4 = "Steel"
        ' add more cases as you like here:

        Case Else
        Me.txtBx4 = "Wrong Code?"
    End Select

End Sub

Note that each Case match has just one action above (change tctBx4 to suit) but you could add lines do other actions e.g. change the font/ colour.

Both codes are in in the attached, revised file.

Hope this solves your problem. If so, please mark this Answer as Selected. Thanks

Discuss

Discussion

Thank you Kindly, John_Ru.
The code working perfectly.
jdgrapes (rep: 16) May 30, '26 at 8:22 pm
Great! Thanks for selecting my Answer, Joe (and for trusting a human to help rather than AI!). 
John_Ru (rep: 6802) May 31, '26 at 2:58 am
Im agree John_Ru
Learn (rep: 2) May 31, '26 at 1:45 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login