Results 1 to 4 of 4

Thread: Adding 2 columns to ComboBox in Form from table

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    7

    Adding 2 columns to ComboBox in Form from table

    Hey guys. Firstly I am beginner, so dont kill me :D

    I have a problem. I have to make 2 column ComboBox in Form. Added data must be from table in sheet, BUT dont know the number of lines, so firstly I have to do that (as u can see in code- that is X)
    Table looks like this
    Name:  ppp.png
Views: 261
Size:  8.5 KB

    Then I tried 2 ways how to add that 2 columns with X lines to ComboBox in Form. This is code:

    Code:
    Private Sub UserForm_Initialize()
     Dim x As Byte
     Dim riadok As Byte
     
      
      x = 1
      While Range("ODBERATELE!B" & x) <> ""
        x = x + 1
      Wend
      MsgBox x
     
     
    
    
    '''''''''''''''''1.WAY - Error 381 :(
       
    For riadok = 2 To x
    
     With UserForm1.ComboBox1
             .List = Sheets("ODBERATELE").Range("A" & riadok & ":" & "B" & riadok).Value
             .ColumnCount = 2
             .ColumnWidths = "50;50"
     End With
       
    Next riadok
    
    
    
    '''''''''''''''''''''''''''''''2.way I have tried has problem with X
    Dim adresy(1 To x, 1 To x) As String
    
    Me.ComboBox1.ColumnCount = 2
    
    For riadok = 2 To x
       adresy(riadok, 1) = Sheets("ODBERATELE").Range("A" & riadok)
       adresy(riadok, 2) = Sheets("ODBERATELE").Range("B" & riadok)
    Next riadok
    
    Me.ComboBox1.List = adresy
     
    End Sub
    Any idea what Im doing wrong, please? :)

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Adding 2 columns to ComboBox in Form from table

    No, but I believe you may be working in VBA, not VB6 (lots of folks get confused on this one).

    I'll notify Moderators...if they deem it be moved, they will...otherwise, someone with more intelligence than I would have to assist you.

    Sammi

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Adding 2 columns to ComboBox in Form from table

    It does look like VBA, so I've moved it to Office Development. If that was wrong, send me a PM.
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Adding 2 columns to ComboBox in Form from table

    this has been answered in the other thread

    this one does have a better title though
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width