Results 1 to 14 of 14

Thread: Cells value to combobox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Cells value to combobox

    I have a combobox on sheet 1 which i need populating with the values of cells on sheet 2.

    So the combobox if possible could have the value or 2 cells in it on the same line for instance sheet 2 column 1 consists of dates and column 2 consists of times, so 1 line in the combobox would look like "22/11/2011 13:45" is that possible?
    And this needs to be a continuous list but it only shows column 1 and column 2 data if column 3 is empty.

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

    Re: Cells value to combobox

    how are you currently populating combobox?
    i presume setting the rowsource should work, else you could fill the combobox using the additem method

    post a sample workbook that demonstrates what you want to achieve
    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
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    Data Store.zip

    Above is the file i have attached for you, There are 2 pages Diary and Entry,
    the combobox on the Entry page needs to be populated with a list created from the diary page columns 1 and 2.

    So a continious list of dates and times to be displayed, but only if there is nothing in column 3 on the same row.

    e.g.

    Column1 Column2 Column3

    22/11/2011 10:00 Hello
    23/11/2011 11:00
    24/11/2011 12:00
    25/11/2011 13:00 Hello

    So in the combobox on the entry page you would see

    23/11/2011 11:00
    24/11/2011 12:00

    So as you can see the combobox only detects rows of data that havent been complete past column 3.

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

    Re: Cells value to combobox

    vb Code:
    1. Private Sub Workbook_Open()
    2. Sheets("entry").ComboBox1.Clear
    3. For Each c In Sheets("diary").Range("a:a")
    4.     If IsEmpty(c) Then Exit For
    5.     If IsEmpty(c.Offset(, 2)) Then
    6.         With Sheets("entry").ComboBox1
    7.             .AddItem c
    8.             .List(.ListCount - 1, 1) = Format(c.Offset(, 1), "h:nn")
    9.         End With
    10.     End If
    11. Next
    12. End Sub
    you will need to set enough columns in the combobox, with appropriate widths
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    Works a treat thank you very much pete, just quickly though when a selection is made from the combobox the displayed selection only shows the date can i get it to show both columns?

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

    Re: Cells value to combobox

    can i get it to show both columns?
    can you see both in the dropdown list?
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    Yes they are both in the list but when you select one it only displays the first column in the combobox

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

    Re: Cells value to combobox

    you can only display one column in the text area of a multi column combobox, but you can change which column you display

    or try like
    vb Code:
    1. Private Sub ComboBox1_Click()
    2. With ComboBox1
    3.     .Text = .List(.ListIndex, 0) & vbTab & .List(.ListIndex, 1)
    4. End With
    5. End Sub
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    Thanks pete, i have realised that only one column can be displayed after selection so that is fine.
    May i ask for another slight bit of assistance, after i have made selection i want my data to go back to that line of.

    So i populate the entry page as you see, make a selection from the drop down then press submit this will then place my data on the same row as the selection i made from the comobox.

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

    Re: Cells value to combobox

    as the list is not contiguous from the sheet, you can not use an index to return the row

    i would suggest adding the row number to an additional column (visible or not) in the combobox

    the alternative would be a double find, find date, then find time staring at date
    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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    Thats thrown a spanner in the works right there, nothing is ever straight forward

    I think if used a userform this wouldnt be an issue, but i didnt want that route as its not really needed.

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

    Re: Cells value to combobox

    I think if used a userform this wouldnt be an issue
    i don't believe it would make any difference at all, just add a column to the combo and add the row number

    vb Code:
    1. With Sheets("entry").ComboBox1
    2.             .AddItem c
    3.             .List(.ListCount - 1, 1) = Format(c.Offset(, 1), "h:nn")
    4.             .list(.listcount -1, 2) = c.row    ' add this line
    5.         End With
    if you want to conceal from users make column width = 0

    that way you can always return the row number
    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

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2009
    Location
    England!
    Posts
    364

    Re: Cells value to combobox

    So the code below send the data to the sheet, but where do i implement the c.row so it goes back to the row it came from please?

    vb Code:
    1. Private Sub commandbutton1_click()
    2.     nrow = Range("a65536").End(xlUp).Row + 1
    3.     col = 1
    4.         Do Until IsEmpty(Sheets(2).Cells(nrow, col + 3))
    5.             nrow = nrow + 1
    6.         Loop
    7.  
    8.     Worksheets(2).Cells(nrow, col + 2).Value = Sheets(1).Range("C3").Value
    9.     Worksheets(2).Cells(nrow, col + 3).Value = Sheets(1).Range("C5").Value
    10.     Worksheets(2).Cells(nrow, col + 4).Value = Sheets(1).Range("C7").Value
    11.     Worksheets(2).Cells(nrow, col + 5).Value = Sheets(1).Range("C9").Value
    12.     Worksheets(2).Cells(nrow, col + 6).Value = Sheets(1).Range("C11").Value
    13.     Worksheets(2).Cells(nrow, col + 7).Value = Sheets(1).Range("C13").Value
    14.     Worksheets(2).Cells(nrow, col + 8).Value = Sheets(1).Range("C15").Value
    15.     Worksheets(2).Cells(nrow, col + 9).Value = Sheets(1).ComboBox1.Value
    16.         On Error GoTo 0
    17. End Sub

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

    Re: Cells value to combobox

    try
    vb Code:
    1. nrow = combobox1.list(combobox1.listindex, 2)

    note: lines 4 to 6 in your code should not be doing anything useful
    or change to column d for your endrow
    Last edited by westconn1; Dec 9th, 2011 at 11:02 PM.
    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