Vba listbox RowSource dynamic range

So I need to fill a list box with information from cells C2:G2

It can go past G2 depending on the previous user inputs

So far I have

I know this is not correct, but I have no idea which part of the code to change

  • #1

    Hi, I am having a problem with setting a listbox rowsource to a dynamic named range. I am using OFFSET to make these dynamic names. The program I am developing is fairly in depth and I am struggling to get this to work. My listbox is in a Userform and it haas multiple columns. I have tried setting the rowsource of a particular listbox directly to the name of the dynamic range: ListBox1.RowSource = "DynamicRange" with an example dynamic named range [with the range being called Transformers, and the particular worksheet it is accessing is TranWS]: TranWS.Names.Add Name:="Transformers", RefersTo:= _ "=OFFSET[$A$2,0,0,COUNTA[$A:$A],7]" TranWS.Names["Transformers"].Comment = "" But setting the rowsource this was has given me the "rowsource invalid property value" error. However, with an older program it did work for some odd reason. I have heard around that you cannot set RowSource to an OFFSET dynamic range, but I did have it working once.... More info on my project is that I have multiple sheets, multiple listboxes, and these listboxes need to access info dynamically from the different lists. I have the listboxes in a userform with buttons to edit and add new items to the the lists [but these aren't added yet]. If anyone has an idea on how to use OFFSET named ranges with a listbox rowsource, please let me know. Thank you for your help. Extra Info: I have one module that creates the sheets, the headers to the columns on the sheets, and the dynamic ranges. This then calls the userform to open: the userform has a total of 6 listboxes and each a different number of columns. The column headers are on, as I want the listbox to grab the headers from the excel sheets. I have the column counts with the correct ammounts, and the dynamic ranges match this number. As seen above, the ranges are set specifically to their sheet [would I need to activate the worksheet I want, or even somehow use the ws name to get the named range [hypothetically: WS.Names["DynamicRange"]]]

    If you need images or more code/explanation, please ask.

  • #2

    Actually, I just found the problem: you cannot set a Listbox to a dynamic range that has nothing in it. Once I put something in the fields the Listbox auto filled and all was merry. I feel stupid since this is obvious but I completely overlooked it. If anyone has an idea on how to set the range at the beginnning of the program without having to put some temp value in the cells, that would be awesome. By the way, if you are confused on why I want them blank it is because the userform will allow people to add items [or in my case electrical components] to an empty sheet and it will record them in an orderly fashion. I am doing this because this info then needs to be formatted very specifically so it can be imported into a oneline electrical software.

    Thanks

    1. #1

      Register for a FREE account, and/
      or Log in to avoid these ads!

      HI, I have encountered an error 'runtime 380' while trying to have a listbox update via dynamic range in a userform. It seems the problem I have is when there is no information in the dynamic range. When I put a test line into the range the listbox populates with the test. Can you assist with this problem? relevant information: Dynamic range "group1": =OFFSET[$E$5,0,0,COUNT[$E:$E],5] Combobox1 [sheet selection][used to populate the ROWSOURCE in the listbox]: Sub combobox1_click dim x as integer x = combobox1.listindex select case x case is 0 listboxname.rowsource = "group1" end select

      end sub

    2. #2

      Please wrap your code with code tags. Thank you

      Thank you Ken for this secure forum.

    3. #3

      And never use Rowsource:

      Code:

      Sub combobox1_click if combobox1.listindex=0 then listbox1.List =cells[5,5].resize[100].specialcells[2].resize[,5].value end sub

    4. #4

      Hi,

      Thank you for your replies.

      Pecoflyer: I am not sure what you mean to wrap the code. I am assuming its the same as the example that snb has provided. If yes, I am not sure how to do this. I am relatively new to this forum and have not been exposed to all its intricacies. I typed the code in.

      SNB: Could you explain why I shouldn't use rowsource? The tutorial I was following suggested this as the correct model to follow. I am not sure what the code you have written is doing exactly. I appears that its referring to row 5 column 5 and not sure what the rest of the code is implying. Combobox1 is designating a sheet for the rest of the user form and in the code for the combobox1 change it refers to the dynamic range from each sheet depending on what selection was made. Is the code you provided creating a dynamic range that is 5 columns across?

    5. #5

      Click the Go Advanced button in lower right of a reply, then click the # icon to insert the code tags. I would copy and paste code between the tags but I guess you can type it if you like typing. Or, type them: e.g. [code]MsgBox "Hello World!"[/code] but replace []'s with []'s. Most are forums do it that way. Why not just not fill the listbox if it errors? e.g.

      Code:

      Private Sub UserForm_Initialize[] Dim s$ s = "group1" ListBox1.ColumnCount = 5 On Error Resume Next ListBox1.RowSource = s End Sub

      Rowsource is not as flexible as List. I generally like List best but if you want to include row1 column headings, Rowsource can do it.

      Of course Count does numerical data and not string data. Use CountA if you want both strings and number counts. So, E1:E10 might be strings so count is 0 and it would error. If E11=1, then count is 1 so E2:I2 is shown in the listbox.

    6. #6

      Originally Posted by Shoeboxken

      Hi,

      Thank you for your replies.

      Pecoflyer: I am not sure what you mean to wrap the code. I am assuming its the same as the example that snb has provided. If yes, I am not sure how to do this. I am relatively new to this forum and have not been exposed to all its intricacies. I typed the code in.

      You will find answers in the FAQ

      Thank you Ken for this secure forum.

    7. #7

      Hi Kenneth, Thank you for the reply. If you have the time, please have a look at the project I am working with and if the code you suggested is applicable. Not all the buttons are working as I have not placed any code into them. The userform will open and allow selections but no addition or clearing. I had to place a dummy line of data in the Sheets where the BGList is applied so that it would populate the Listbox. Regards Ken

      UserForm.xlsm

    8. #8

      If you want your listbox to update, then change the Rowsource values in that worksheet. You need to refer to your dynamic named range using the sheet's name.I also added the Clear button code which triggers the clear in the combobox change.

      Code:

      Private Sub ComboBox1_Change[] Dim x As Integer x = ComboBox1.ListIndex Select Case x Case Is = 0 lstBG.RowSource = "BattleGroup1!BG1List" Case Is = 1 lstBG.RowSource = "BattleGroup2!BG2List" Case Is = 2 lstBG.RowSource = "BattleGroup3!BG3List" Case -1 lstBG.RowSource = "" End Select End Sub Private Sub CommandButton2_Click[] ComboBox1.ListIndex = -1 End Sub

      Last edited by Kenneth Hobson; 2017-12-04 at 05:54 AM.

    9. #9

      Code:

      Private Sub ComboBox1_Change[] if combobox1.listindex>-1 then lstBG.list=sheet1.columns[11 + 2*combobox1.listindex].specialcells[2] End Sub

    10. #10

      Hi Kenneth,

      Thanks for your reply. I will add the line for the unspecified sheet selection. From what I gather, if there is no lines of data in the dynamic range there will be an error message so there will always have to be one line of data existing in the dynamic range to prevent this. I will just keep the dummy line.
      Many Thanks


      Ken

      Hi SNB,

      I am unsure how the code you have written is intended to work. If you could give me a quick explanation it would be extremely helpful.

      Regards


      Ken

      Last edited by Shoeboxken; 2017-12-04 at 07:14 PM.

    Video liên quan

    Chủ Đề