+ Reply to Thread
Results 1 to 2 of 2

Add Headers to a Listbox populated from a Textbox

  1. #1
    Registered User
    Join Date
    01-08-2016
    Location
    Santa Cruz, Bolivia
    MS-Off Ver
    2007
    Posts
    1

    Add Headers to a Listbox populated from a Textbox

    Hi everyone,

    I'd appreciate if anyone could give a hand on the next problem:

    Ive got a userform that has 4 textboxs, a combobox, two command buttons and a MULTICOLUMN listbox.
    The listbox is populated from the textboxes/combobox after the commandbutton1 is clicked. My problem is Im trying to add headers to the listbox, however ive seen in previous threads that since the listbox is populated from textbox and not from a worksheet this cant be done, is this true? If so, how can I add a first row that works as header.

    The listbox has 5 columns and I need the headers to say:
    P/N---Description----Qty---M.U.---Batch

    HOWEVER, i have a second commandbutton that allows the user to DELETE a row selected from the listbox, so I need to lock the possibility of the user to delete the first row but delete any other row he selects. I cant use labels cause Ive written the code so that the columns autosize and so if I put a Label o top of the Listbox its never well positioned.

    heres my code if it helps.



    Private Sub CommandButton1_Click()



    ListBox1.AddItem TextBox1.Value
    ListBox1.List(ListBox1.ListCount - 1, 1) = TextBox2.Value
    ListBox1.List(ListBox1.ListCount - 1, 2) = TextBox4.Value
    ListBox1.List(ListBox1.ListCount - 1, 3) = TextBox3.Value
    ListBox1.List(ListBox1.ListCount - 1, 4) = ComboBox1.Value






    ' From here on its the autosize code


    With ListBox1
    .ColumnCount = 5


    For i = 1 To .ColumnCount
    With Me.Controls.Add("Forms.TextBox.1", Name:="txtTemp" & i)
    .AutoSize = True
    .MultiLine = True
    .WordWrap = False
    .SelectionMargin = False
    With .Font
    .Name = ListBox1.Font.Name
    .Size = ListBox1.Font.Size
    End With
    End With
    Next i

    For i = 0 To .ListCount - 1
    Me.Controls("txtTemp1").Text = Me.Controls("txtTemp1").Text & vbCr & .List(i, 0)
    Me.Controls("txtTemp2").Text = Me.Controls("txtTemp2").Text & vbCr & .List(i, 1)
    Me.Controls("txtTemp3").Text = Me.Controls("txtTemp3").Text & vbCr & .List(i, 2)
    Me.Controls("txtTemp4").Text = Me.Controls("txtTemp4").Text & vbCr & .List(i, 3)
    Me.Controls("txtTemp5").Text = Me.Controls("txtTemp5").Text & vbCr & .List(i, 4)
    Next i

    For i = 1 To .ColumnCount
    StrCwidths = StrCwidths & Me.Controls("txtTemp" & i).Width & ";"
    lngTotalWidth = lngTotalWidth + Me.Controls("txtTemp" & i).Width
    Me.Controls("txtTemp" & i).Visible = False
    Me.Controls.Remove ("txtTemp" & i)
    Next i

    .Width = lngTotalWidth + ListBox1.ColumnCount + 6
    .ColumnWidths = StrCwidths
    End With






    End Sub

    Thanks a lot in advance for the answers.

  2. #2
    Forum Expert
    Join Date
    03-20-2015
    Location
    Primarily UK, sometimes NL
    MS-Off Ver
    Work: Office 365 / Home: Office 2010
    Posts
    2,405

    Re: Add Headers to a Listbox populated from a Textbox

    Welcome to the forum.
    Please edit your post to add CODE tags to your code - this makes it easier to read and also formats it correctly - select the code and click the hash # icon. Thank you.

    I'm not a VBA expert, but I wonder whether you might be able to do what you want by putting the header in another Listbox, which is positioned above the main one. You could then allow deletion of rows from the main listbox, but lock the header listbox. For the columns to be the same width, after autosizing the columns in the main one, I think it must be possible somehow to format the column widths in the header listbox to be the same width as the ones in the main listbox.
    Having seen quite a bit of code over the years, I think something like this must be possible - but there's no way I could write it for you, sorry!
    Regards,
    Aardigspook

    I recently started a new job so am a bit busy and may not reply quickly. Sorry - it's not personal - I will reply eventually.
    If your problem is solved, please go to 'Thread Tools' above your first post and 'Mark this Thread as Solved'.
    If you use commas as your decimal separator (1,23 instead of 1.23) then please replace commas with semi-colons in your formulae.
    You don't need to give me rep if I helped, but a thank-you is nice.

+ 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. Equivalent to a populated InputBox or pop up Listbox
    By ARowbot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-18-2015, 12:30 PM
  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. [SOLVED] Populated Textbox by listbox only showing first record
    By roelof bakker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-27-2014, 09:17 AM
  4. [SOLVED] Textbox to be populated with data from another worksheet
    By saji in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2013, 07:02 AM
  5. Complex userform with user-populated listbox
    By olivierpbeland in forum Excel Programming / VBA / Macros
    Replies: 72
    Last Post: 01-23-2013, 04:18 PM
  6. Error in populated listbox userform
    By herukuncahyono in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2010, 09:54 PM
  7. Add Items To RowSource Populated ListBox
    By radar_jynx in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-14-2009, 10:18 AM

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