VBA Userform- autofill textboxes if reference already exists in the sheet

  • One major issue that I have found is not VBA related. Your Table(Listobject) extends way beyond the recorded data, so lr is returning 499 in the frmAdd code. Your columns are the same.

    I defined the table to range A1:E500 for the sample file I attached here.

    But the final table will have even more columns and rows...

    If this is a problem then I should tell the code to only look in the occupied cells, but how do I do that?

    Thank you again

  • This is how I would write the code to add a line


  • You don't need empty rows and columns in a Table. As you add them then the Table resizes.


    Good reading is


    Overview of Excel tables

    Ok, noted, read the link. BUT, the frmAdd works perfectly without my TB_Change_Event, even with the table sized that way, and with the emtpy rows.

    SO it must be my change event code in the TB Client that is incorrect, I missed something or gave the wrong ranges to look into...

  • This is how I would write the code to add a line



    Which form is your last posted code in?

    In frmAdd.

    I tried to put your code into the frm Add but it highlights ctrl and Private sub line...

  • Probably because you haven't used Option Explicit. You also need to resize the Table

  • Your code does not appear to be using a ListObjects properties correctly. Have a close look at how I added a new entry in the code above.

    I did, thank you very much. You defined the Listobject as oTbl.

    But your code does not add the new row under the last row occuied by that specific client, and that would be a major issue for me.

    I know I somehow have to add a AND (do add the row under the last one occupied by than client's name AND also fill the QUality and Count textboxes with the values of lr, 4 and lr, 5.

  • Then you need to insert the row at a specific point or probably more simply sort the data. I'm not sure where your code was actually posting the entries to.

  • Probably because you haven't used Option Explicit. You also need to resize the Table

    I did use Option explicit.

    Also, I downloaded the file you attached, still the debug highlights the ctrl and does not let me enter the new row from the userform, not does it autofill textboxes.

    CAn you please help with the change event, some suggetions if possible?

    Thank you

  • Where is this change event?


    I can see what is happening now. I added Option Explicit just before uploading. You haven't declared ctrl as a variable. You need to add


    Code
    Dim ctrl As MSForms.Control
  • Where is this change event?


    I can see what is happening now. I added Option Explicit just before uploading. You haven't declared ctrl as a variable. You need to add


    Code
    Dim ctrl As MSForms.Control

    Ok, with the crt variable added it works, but it has eliminated the most important feature of the code : it add the new client row to the next available row in the table, whereas my original code added an already exisiting client to the ow below the last one of that client.

    I realize I posted a lot and maybe my request got lost.

    I need the code IF the client name inserted in TB-CLient has already been recorded in the sheet, to also autofill the textboxes QUality and Count with the values already registerd in the last row with that client name (client name is always in column A)

  • I'm sorry but that wasn't clear.


    Obviously we have different ideas about perfect code.


    Again, I would simply load all the clients data into a Listbox and select that, the only way that I can think of achieving this is to loop through each of a clients entries and find the last one. You are talking about frmAdd?


    I can see in your code that someone else has pointed out the need for identifiers.


    I don't know where you want to add this in your code but based on the Loop that finds Brad's last entry this does what you want using RowCount


    Code
    Me.TB_Cases.Value = ThisWorkbook.Sheets("Clients").ListObjects("tblClients").DataBodyRange(RowCount, 1).Value
                Me.TB_Quality.Value = ThisWorkbook.Sheets("Clients").ListObjects("tblClients").DataBodyRange(RowCount, 4).Value
                Me.TB_Count.Value = ThisWorkbook.Sheets("Clients").ListObjects("tblClients").DataBodyRange(RowCount, 5).Value

    From this you can see why I would declare a variable for the ListObject - it saves typing!

  • Hi RoyUk hi everyone, have a nice Monday.

    I just wanted to let you know I created myself a Change_event n my userform, as your solution below, when inserted in the code, below the For Loop, it worked but of course it filled the txboxes only when I clicked the butt. Add (logically, as the lines were in the code of the Sub Butt_Add_Click.

    Code
    me.textbox.name=ThisWorkbook.Sheets("Sheetname")ListObjects("tblClients").DataBodyRange(RowCount, 1).Value

    What I did I created and extra Combobox on the useform frmAdd, and I gave this CMBX a CB_CHange_Event

    I post the code might help somebody else too.

  • I forgot to add the Sub Initialize I had to use for the CBX, befor the change_event code. Sorry, here it is

    Code
    Private Sub UserForm_Initialize()
    'Define the range variable
    Dim xRg As Range
    'Define the target column/range where the CB has to look for values to assign to its list    Set xRg = Worksheets("Sheetname").Range("A:A")    Me.ComboBox1.List = xRg.Columns(1).Value
    End Sub 
  • I don't think that your code has copied correctly to the site. I think it should be


    Code
    Private Sub UserForm_Initialize()
    'Define the range variable
    Dim xRg As Range
    'Define the target column/range where the CB has to look for values to assign to its list
    Set xRg = Worksheets("Sheetname").Range("A:A")
    Me.ComboBox1.List = xRg.Columns(1).Value
    End Sub

    However, that doesn't make sense because it seems to be loading the whole of Column A.

  • Thank you for the correction RoyUk, yes, the set line was on the same line with the comment, error from copy/paste code.

    As for the whole column thing, that is correct, but it is the only solution I could think of and put in practice. As per my very frist posts in this thread I had been asking for a change event code, that is what I needed.

    Of course the solution I found is "rough", but i am a beginner; and it works.

    Should I find another solutin or understand how to tweak the present Initialize Sub in order for it to load only the names I type in the CMBX, I will post. it.

    If you have any suggestions, or anyone else, of course it would be highly appreciated. :)

    Thanks anyway

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!