+ Reply to Thread
Results 1 to 7 of 7

column heads in excel vba

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2008
    Posts
    4

    column heads in excel vba

    Im trying to add coloumn heads to my listbox but its just not working
    i was using rowsource to use the first row of the sheet as the headings however this just set the values in the listbox to the rowsource

    Here is my code

    If Area = "" Then
    
    Dim c As Range
        
           Zip = "*" + Zip + "*"
        
        Me.ListBox1.Clear
    
        For Each c In Range([e2], [e65000].End(xlUp))
            If UCase(c) Like UCase(Zip) Then
                With Me.ListBox1
                     .AddItem c
                     .List(.ListCount - 1, 0) = c.Offset(0, -4).Value
                     .List(.ListCount - 1, 1) = c.Offset(0, -3).Value
                     .List(.ListCount - 1, 2) = c.Offset(0, -2).Value
                     .List(.ListCount - 1, 3) = c.Offset(0, -1).Value
                     .List(.ListCount - 1, 4) = c.Offset(0, 0).Value
                     .List(.ListCount - 1, 5) = c.Offset(0, 1).Value
                     .List(.ListCount - 1, 6) = c.Offset(0, 2).Value
                     .List(.ListCount - 1, 7) = c.Offset(0, 3).Value
    
                 End With
            End If
        Next c
             
            
      Else
    
        Dim b As Range
        
           Area = "*" + Area + "*"
    
        Me.ListBox1.Clear
        For Each b In Range([F2], [F65000].End(xlUp))
            If UCase(b) Like UCase(Area) Then
                With Me.ListBox1
                     .AddItem b
                     .List(.ListCount - 1, 0) = b.Offset(0, -5).Value
                     .List(.ListCount - 1, 1) = b.Offset(0, -4).Value
                     .List(.ListCount - 1, 2) = b.Offset(0, -3).Value
                     .List(.ListCount - 1, 3) = b.Offset(0, -2).Value
                     .List(.ListCount - 1, 4) = b.Offset(0, -1).Value
                     .List(.ListCount - 1, 5) = b.Offset(0, 0).Value
                     .List(.ListCount - 1, 6) = b.Offset(0, 1).Value
                     .List(.ListCount - 1, 7) = b.Offset(0, 2).Value
    
                 End With
            End If
        Next b
        
    End If
    Any help would be much appreciated

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Not sure what your code is doing, but have you set the ListBox's ColumnHead Property to True?

    Me.ListBox1.ColumnHeads = True
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    06-11-2008
    Posts
    4
    I use the following code

    With Me.ListBox1
    .ColumnHeads = True
    .RowSource = "A2:H2"
    'End With

    But this just puts row 1 in the listbox and not the values i want to display under the headings

    How can i use the headings from row A1 to H2 without wiping out the values i display in the listbox?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try

    Private Sub UserForm_Initialize()
    With Me.ListBox1
    .ColumnHeads = True
    .RowSource = Range(Cells(2, 1), cells9rows.Count, 8).End(xlUp).Address
    'End With
    End Sub

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461
    Cheers
    Andy
    www.andypope.info

  6. #6
    Registered User
    Join Date
    06-11-2008
    Posts
    4
    it's throwing an error

    "wrong number of arguments or invalid assignment"

    i dont really understand the rowsource line of code so i cant see where it's going wrong

    thanks so much for the help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1