+ Reply to Thread
Results 1 to 7 of 7

Adding column headers to listbox

Hybrid View

devatu Adding column headers to... 08-26-2015, 04:27 AM
Greg M Re: Adding column headers to... 08-26-2015, 06:04 AM
devatu Re: Adding column headers to... 08-26-2015, 06:40 AM
Norie Re: Adding column headers to... 08-26-2015, 06:53 AM
devatu Re: Adding column headers to... 08-26-2015, 07:21 AM
Norie Re: Adding column headers to... 08-26-2015, 07:38 AM
devatu Re: Adding column headers to... 08-26-2015, 07:44 AM
  1. #1
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Adding column headers to listbox

    Hello all,

    I just found the columnhead function and set it to TRUE.
    But I didnt know how to add information in the headers so I tried to look it up on the internet.
    Soon it became clear to me that you need to use the RowSource for it and that it can only be done by using a range from an excel worksheet and not from within the vba code itself.
    This is all fine to me, but when I have data in a sheet called sheet1, and use the following code:
    Generate_Add_Form.ListBox1.RowSource = "A2:G2"
    it will show the names I have in the excelform as if it was the first record on my listbox, and the rest of my records have disappeared. So it is not in the columnheaders above it.
    Does anyone know how to fix this?

    Thanks in advance and cheers,

    Devatu

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Adding column headers to listbox

    Hi there,

    If the values for your header texts are contained in Cells C5:E5 and your data values are in Cells C6:E10, you should define the RowSource Property of your ListBox as $C$6:$E:$10. The Column Headers for the ListBox will be taken from the row above this range.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M

  3. #3
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Adding column headers to listbox

    Hey Greg,

    Thanks for your quick reply.

    I populate the listbox by data from my Access database, so not from data in a excel workbook.
    The only thing I have in my excel workbook are the names for the headers, so not the actual data itself.
    Is it not possible to do it than?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Adding column headers to listbox

    No, as you stated in your original post the only way to have column headers in a listbox is to populate it using RowSource.

    There are alternatives however, for example you could create a set of small labels positioned above the columns of the listbox, or you could add another one row listbox positioned above the main listbox and populate with the field names.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Adding column headers to listbox

    Hmm okay, that's a pity then. Thanks for another helpful reply Norie.

    A second question ( I don't know if I should make another thread for this, I suppose not.)

    I have the following code:

    Private Sub CommandButton7_Click()
    	Dim Filepath As String
    	Dim Filename As String
    	Filename = InputBox("Please give a filename for the FMECA file.")
    	Filepath = "C:\Users\Devin\Desktop\" & Filename & ".xlsx"
    	Workbooks.Add.SaveAs (Filepath)
    	With Workbooks(Filepath).Sheets("Sheet1")
    		.Range("A1") = "test"
    	End With
    End Sub
    But it gives me an error, with something about that the subscript is out of reach.

    What I want to do, is open a new excel file, the user gives an input name and then I want to write something in a cell in the newly made workbook.
    Everyone on the internet says that you have to use a range or cells, but I can't manage to get it working..

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Adding column headers to listbox

    Really should be a new thread, but the answer is straightforward.

    When using Workbooks(...) you only need the workbook name, Filepath includes the path and filename.

    Try replacing Workbooks(FilePath) with Workbooks(FileName & ".xlsx").

    Or create a reference to the new workbook.
    Dim wbNew As Workbook
    
        ' other code
    
        Set wbNew = Workbooks.Add
    
        With wbNew
            .SaveAs Filepath
    	With .Sheets("Sheet1")
                .Range("A1") = "test"
    	End With
        End With

  7. #7
    Registered User
    Join Date
    08-05-2015
    Location
    Netherlands
    MS-Off Ver
    2013
    Posts
    39

    Re: Adding column headers to listbox

    Thanks for the reply Norie, it works like a charm.

    Sorry for not posting it in a new thread.

    Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] "Custom" entry of column headers for a userform listbox?
    By CaptainK in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-26-2014, 09:41 AM
  2. [SOLVED] listbox with column headers as a specific sheet column headers
    By ANDREAAS in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-07-2014, 04:40 AM
  3. Adding multiple column values using listbox
    By mayurpatil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-27-2014, 12:33 PM
  4. [SOLVED] Adding new items to Multi-Column Listbox on seperate Userform
    By Hutinni in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2013, 02:43 PM
  5. [SOLVED] How to show column headers in listbox from access database?
    By Kiran2012 in forum Access Programming / VBA / Macros
    Replies: 4
    Last Post: 11-26-2012, 10:30 AM
  6. [SOLVED] Adding more data to column 2 in Listbox
    By coreytroy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-19-2012, 01:21 AM
  7. [SOLVED] Listbox column headers when data is collected in an array
    By evertjvr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-12-2012, 09:44 PM

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