+ Reply to Thread
Results 1 to 26 of 26

adding values to a combobox from a specific range of cells

  1. #1
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    adding values to a combobox from a specific range of cells

    Hi, I am trying to write the code for a combo box in an entry form such that selecting a value from from drop down menu displays specific values in a second combobox.

    For example, the options in the first combobox would be "Today, Last Week, and Last Month". Selecting one of these options would display in a second combobox the values in the Name column of hte worksheet, corresponding to the respective dates. Once this name was chosen, the data entered in the rest of the form woudl be entered into the row with that name.

    Could someone please show me how to do this? Thank you!

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: adding values to a combobox from a specific range of cells

    Try the attached.

    Dion
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I appreciate your help, and that's definitely in the right direction, but I'm trying to do something a little different. I apologize if I wasn't clear enough in my last post.

    I've attached an example spreadsheet. If, for example, I were to select "Last Week" in the first combo box, what I would like to be able to do is pick a name from a second combo box from any of the items that have a corresponding date in the correct period, and for the data entered in the entry form to then be added to that row.

    Any help at all would be greatly appreciated! Thank you!
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    315

    Re: adding values to a combobox from a specific range of cells

    I've modified it to copy the name rather than the date.

    Dion
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I'm trying to edit the code to match it to my worksheet, but I'm having trouble. would it be possible for you to tell me what each line in the code does, so that I might have a better idea of what I'm supposed to do? Thank you!

  6. #6
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I've been able to adapt part of the code, and am working getting the rest of it, but I ran into an issue.

    If I select a date in the combobox, a number comes up in the box rather than the date. How can I program the combobox to display it as a date value?

    For example, if I were to select "Today", and then select today's date (7/21/2010), the value "40380" is displayed in the combobox. Is there a way to make it display "7/21/2010"?

    The section of hte code that I think i should be looking at to do this is:

    Please Login or Register  to view this content.
    Thank you!!

  7. #7
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    blindzero678;

    I'm coming in late and I can't figure out where you're at. The workbook that I downloaded didn't have any entry form that I could find.

    But I did find this to work to put a date in cboDate.
    Please Login or Register  to view this content.
    In the file I was looking at, it was right above "Private Sub cboPeriod_Change()"

    If you post your workbook now, I may be able to help.
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

  8. #8
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I actually don't see that line of code anywhere in either of the files that were posted before? Anyway, I tried pasting the code into my form, but the date was still displayed as the number "40382" once it was selected from the drop down menu.

    I thought it might be easier if i attached the actual userform that I am trying to develop though.

    There are three comboboxes on the userform that are not in the multipage control. In the first of these, I want to be able to select a period (last month, last week, today), and for the dates in that range that appear in the database to appear in the second combobox. From what i've done so far, i've been able to get today's date to come up in the second combobox. However, if i select today's date, 7/23/2010 isn't displayed in teh box. instead "40382" is displayed.

    I also still can't figure out why the dates for "last month" and "last week" do not appear in the second combobox

    also, if this makes a difference, once I select the date, i would like the third combobox to display the names that appear in the database that correspond to that date.

    I really appreciate your help!
    Attached Files Attached Files
    Last edited by blindzero678; 07-23-2010 at 10:09 AM.

  9. #9
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Hi blindzero678;

    In the file you just uploaded, ComboBox_Period_Change() is populating ComboBox_StudyDate with names. So I'm putting dates into it. Then putting Names into ComboBox_Name. Should be ready within the hour.

  10. #10
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Ok, here's a file that populates the date combobox with correct dates, and name combobox with correct names.

    I put in #s as names for testing, and dates from 45 days ago for testing. I had it disable the date combobox if you choose today.

    I added a module named "FoxGuy". In it is a function "Definitions") where you can change the variables that were used in the date and name combobox_change(). If you redesign your workbook (change sheet names, move columns, insert rows above the headings, etc), you just need to change the addresses in that function and the change events will automatically adjust.

  11. #11
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    Wow, this is great! Thanks so much!

    Could you show me how to remove the duplicates in the Date combobox, however?

    And also, what would I do if I wanted to populate the userform with the data for the patient that's chosen with the comboboxes? I think if you could jsut show me how to get started, I should be able to go from there.

    Thanks again! You've been a huge help!

  12. #12
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    blindzero678;

    Here's a new file.

    It didn't even occur to me that the dates would have duplicates. That's what debugging is for.

    I like to use the same sub to copy data from the spreadsheet to the form, and from the form to the spreadsheet. That way it's easier to verify that I have not missed any fields. I just pass it a parameter that tells it what direction to transfer data.

    In the Foxguy module the sub "Transfer_Data" illustrates what I mean. There are 2 alternative methods to transfer data. You pick the one you like and delete the other one.
    It's called from the ComboBox_Name_Change() to load the form. I would call it from the end of CommandButton_OK_Click() to save the data to the sheet.

    I personally don't like having comboboxes empty. When ComboBox_StudyDate is filled by ComboBox_Period_Change(), I would like to fill ComboBox_Name also, but it triggers the ComboBox_Name_Change() and fills the form. I commented it out in ComboBox_StudyDate_Change(), so ComboBox_Name is blank when the dates are put in. If it was my form, I might consider having it automatically loading the form anyway, but that's your decision.

  13. #13
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I'm getting an error message when I select a period now. The message says "Compile error: Can't find project or library" and in the line

    Please Login or Register  to view this content.
    "Date" is selected.


    Also, if it's not too much trouble, would you mind explaining to me what you did? This is the first userform I've made, and I'd really like to understand for future reference.

    Thank you!

  14. #14
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    I'm really at a loss for compile errors. It compiles on my computer, and I believe it worked on your computer at one time. So I'm guessing here.

    If "Date" is highlighted by itself, then it's saying that your computer can't determine what the date is (I think). Try rebooting.

    If the whole line is highlighted, but it used to work, then somehow the file got corrupted (I think). Try downloading from the forum again.
    Also, if it's not too much trouble, would you mind explaining to me what you did? This is the first userform I've made, and I'd really like to understand for future reference.
    It would be easier for me if you post the sections that you don't understand. Trying to go through one line at a time to explain what it's doing could take quite a bit, especially since I don't know how much you already know. I would assume you knew nothing and spend a lot of time trying to explain things you might already know.

  15. #15
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I found the problem with the compile error. There was a reference listed as missing; all I had to do was remove it from the tools menu.

    As for what I don't understand, could you explain these two pieces of code? I think that if I can understand these, I should be able to figure out the rest on my own.

    Please Login or Register  to view this content.
    Once again, thanks for being so helpful!!
    Last edited by blindzero678; 07-25-2010 at 03:20 PM.

  16. #16
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Please Login or Register  to view this content.
    Let me know how I did at explaining things.

  17. #17
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    That explanation helped a lot, thanks!!

    I did run across one more issue though. In transferring the data from Textbox_Min and Textbox_Sec to the spreadsheet, I used a formula to convert the minutes to seconds and to enter the sum as one value in the spreadsheet.

    To transfer this data back to the userform, I need to be able to convert this back to a value in minutes and seconds. I was thinking to use a RoundDown function to divide the seconds value in the spreadsheet (column "AD") by 60, rounding down to the total number of minutes. And after that, I was thinking to use another formula (i'm actually not sure which?) to retrieve the decimal from the value and multiply that by 60, to return the number of seconds. could you show me how to do this?

    thank you!
    Last edited by blindzero678; 07-25-2010 at 05:03 PM.

  18. #18
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I think I worked out a solution to the question I just posted:

    Please Login or Register  to view this content.

    Unless you think another method woudl be more effective?

  19. #19
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Minor Improvement
    Please Login or Register  to view this content.
    1) Makes it easier to debug.
    2) Try not to do the same calculation twice. Most of the time it won't make a difference, but forming the habit will serve you well when the calculations are more complex.

  20. #20
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    Thank you!

    Sorry to keep bothering you, but this should be the last question. I've noticed that if I search for a patient using the three comboboxes that you helped me program, then resubmit the data using CommandButton_OK, everything gets submitted properly, and everything is cleared properly except for the checkboxes. The checkboxes remain checked and are grayed out. However, if I click on one of the checkboxes, it is no longer grayed out, and I can then click it again to uncheck it. Is there something I can do to make them clear automatically, like they are supposed to?

  21. #21
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    I am also now getting a "Run time error '13': Type Mismatch" whenever I click CommandButton_OK

    the line

    Please Login or Register  to view this content.

    is selected under ComboBox_StudyDate_Change()


    do you have any idea of what could be causing this?

    It was working fine before, I don't know what went wrong...
    Last edited by blindzero678; 07-26-2010 at 04:54 PM.

  22. #22
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Hi blindzero678;

    Sorry it took so long to get back to you. I've been sick in bed all week.

    If you're getting "Type Mismatch" error then try using this:
    Please Login or Register  to view this content.
    When a checkbox is grayed out, it means that it has a Null Value. I looked at your code, and it looks like you're setting the value to False (but I didn't really study it), so I don't understand why, but somehow your code is setting the value to null.

  23. #23
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    Hey foxguy,

    Sorry for the late reply, I was out of town and away from my computer. I hope you are feeling better though.

    I added the code that you gave me, and got a "run-time error '91': Object variable or With variable not set" message when I tried looking up a patient name using the comboboxes you helped me program. The following line in Module1 was highlighted:

    Please Login or Register  to view this content.

  24. #24
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Hi blindzero678;

    My bad.
    I believe it's crashing when the name doesn't exist (I assumue you're adding a new name). I'm kind of guessing without studying the whole workbook, but I believe this is the problem.
    It should be changed to this:
    Please Login or Register  to view this content.

  25. #25
    Registered User
    Join Date
    07-08-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2007
    Posts
    24

    Re: adding values to a combobox from a specific range of cells

    That seems to have fixed the run-time error I was getting.

    About the grayed out checkboxes, however. If I search for a patient using the comboboxes at the top of the userform that you helped me program, then click the okay button, all of the checkboxes on the "Patient Data" tab are grayed out and the date "12/30/1899" is displayed (also grayed out) in the middle comobobox of those three. If I just add a new name and set of data however, when I click the okay button everything clears properly.

    This is the code I have right now at the end of the CommandButton_OK_Click() sub to clear everything once all the data has been entered into the worksheet:

    Please Login or Register  to view this content.
    It seems to me that it shouldn't be setting the checkbox values to Null, if I'm not mistaken?

  26. #26
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: adding values to a combobox from a specific range of cells

    Quote Originally Posted by blindzero678 View Post
    It seems to me that it shouldn't be setting the checkbox values to Null, if I'm not mistaken?
    You're right, the code looks right on the checkboxes. On the date, 0 printed with a date format is Dec 31 1899, so you might have to change the formatting to have it be blank. It does seem to me that assigning a value of "" would be enough, but obviously it isn't.

    I would have to see your workbook to track down what is doing it.

    You can try this:
    Put a breakpoint on the line
    Please Login or Register  to view this content.
    ( or put a "Stop" just after it).
    When the code stops, you can bounce back and forth between Excel and VBE to see the effect of every line.
    1st look to see if the checkboxes have been cleared. If they haven't then you know there is something in this code that does it.
    Assuming that they look alright then go back to the VBE and click F8. This will step you through the code one line at a time. And every time it does something go look on the worksheet to see the effect.

    Other than that you will have to post your workbook. Leave a few names and data in the file, but change the names so they are not recognized.

+ 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