Results 1 to 5 of 5

Thread: [RESOLVED] help: retrive column names of a worksheet into a listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Resolved [RESOLVED] help: retrive column names of a worksheet into a listbox

    Hi there,

    I would like to fill my listbox with all the column headers (excel).

    I am wondering if any of these would work

    1) Using a SQL statement to retrieve all the column headers into the listbox

    2) loop to last cell within the first row (excel takes 1st row by default to be column names) and additem to listbox at each loop.. i'd use a range to specify the first row/cells... so that i can refer to the individual cells using an integer.

    my loop will just consist be restricted to x=x+1 till end of last filled cell.

    Kindly advise which method is viable? Anyone has examples to share? Thank you very much@

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

    Re: help: retrive column names of a worksheet into a listbox

    vb Code:
    1. lastcol = range("iv1").end(xltoleft).column
    2. for i = 1 to lastcol
    3.   list1.additem cells(1, i)
    4. next
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: help: retrive column names of a worksheet into a listbox

    BINGO! thank you westconn1

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    90

    Re: [RESOLVED] help: retrive column names of a worksheet into a listbox

    how can i specify the workbook/worksheet which i would like to retrive the column_names from?

    my selectino of workbook/worksheet will be from a listbox..
    e.g > listbox 1 will show all the worksheets available

    by clicking on all the respective worksheet.. another listbox wld return all the column names..

    thank you! =)

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

    Re: [RESOLVED] help: retrive column names of a worksheet into a listbox

    vb Code:
    1. lastcol = sheets(listbox1.text).range("iv1").end(xltoleft).column
    2.       for i = 1 to lastcol
    3.         list2.additem sheets(listbox1.text).cells(1, i)
    4.       next
    you will need to change list2 to the name of the listbox you want to fill
    put the code in the click event of listbox1
    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