+ Reply to Thread
Results 1 to 15 of 15

Each value selected in dropdown fires different macro

  1. #1
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Each value selected in dropdown fires different macro

    Hi you'll,

    Plese see attachment for wb.
    I am creating wb for teachers that helps them to keep track of the grades from their students.
    Sheet 1 has the student info and the subjects given.
    Sheet 2 has a lot of info copied from sheet 1 (through simple formulas) and here the actual grades are entered.
    The average grade is calculated, which will be used to write in the report cards.

    Sheet 2 has some codes that hides columns and rows which are not needed. When a student name or a subject is not entered in sheet 1, the columns and rows concerning the sudent and subject are hidden (why show when it is not needed?)
    All these codes work fine thanks to members of this and other forums.

    I am now working on a macro and/or code to print the results from the students. Im have created a drop down (form control) which lists all the students. What I like to achieve is that whenever a student is selected from the drop down, the print macro fires to print the info from the selected student. See macro below, which works great.
    Please Login or Register  to view this content.
    Before this macro fires, I like the user to see the print options.
    See below (thanks to RoyUK). Also this works fine.
    Please Login or Register  to view this content.
    Seperate the 2 work fine. But how can I integrate the 2 to work togetter? I found some code that uses case select. But the student names might change during the course of a trimester due to whatever reason.

    Any ideas to help me in the right direction are highly appreciated.

    marcoAUA
    Attached Files Attached Files
    Last edited by MarcoAUA; 03-11-2012 at 04:25 PM.

  2. #2
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    What I like to use in such situation is the Print Preview which gives user much flexibility. Change the last line of program to:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Merci bien monsieur (here my French stops).
    I'll try it out when I've access to the file.
    Any thoughts about combining these 2?

    marcoAUA

  4. #4
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    It would be something like:
    Please Login or Register  to view this content.
    Last edited by p24leclerc; 03-08-2012 at 10:15 PM.

  5. #5
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Monsieur, thanks for the fast reply.

    I tried your code, put it in modlue 1 and debugger kicked in on
    Cancel = True
    Compile error; Variable not defined.

    Any thoughts?

  6. #6
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    I think you can delete this line. The error comes probably from the fact that you have the expression " Option Explicit" at the beginning of your macros and this is fine. This means you out to declare any variables. As variable Cancel was not declared, you got the error.
    If you look at the original sub from RoyUK, you see that Cancel is a variable parameter from the sub itself. You do not need it in yours.
    Regards

  7. #7
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Hi again,

    This works great.
    When the print button was clicked, first I got the PrinterSetUp dialog box and later the print preview.
    Since also the print preview gives you the option to change the settings, I deleted.
    Application.Dialogs(xlDialogPrinterSetup).Show.

    When clicked, you go to preview, from there you can change the printer settings and off you go!
    Please Login or Register  to view this content.
    Back to the first question, how do I connect this macro to a value from a drop down list (form control), or any other drop down?

    Thanks,

    marcoAUA.

  8. #8
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    Right click on your drop down list and select "Assing Macro". From the opened window, select the macro you want to run when the list is used. It can be the same as the print button "SetPrintRangeStudent1" or any other one you want.

  9. #9
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    The macro will be assigned to the drop down, not to the values stipulated in that drop down. Also the values in this drop down will cange:
    the student names might change during the course of a trimester due to whatever reason.
    .
    Is there a way to make this happen? Do you refer instead of the students name to the cell?

  10. #10
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    See attached file. I hope it works the way you want it to.
    I had to set cell C1 as the cell link to the listbox.
    I also added a formula in cell D1 (under your listbox) to get the student name from the rank given by the listbox.
    You can see the formula if you click on cell D2 and move up ont time with the arrow.
    I modified the macro SetPrintRangeStudent1 to work as you wish.
    I commented the macro lines so you could understand how it works or what is its purpose.
    You'll have to arrange your sheet very precisely. I mean each range to print is 12 columns wide and the starting point is 3 rows above the student's name cell. You'll have to set all titles properly. Lots of work.
    Hope this help
    Attached Files Attached Files

  11. #11
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Hello again!

    I am very impressed! This is what I was looking for.
    Just for me understand this whole thing better, a few questions:
    1.Why did you had to set cell C1 as the cell link to the listbox? Is this that the sequence of the values in the drop down are assigned a number which make the macro run?
    2.What does the formula in D1 do?

    One thing happened: when a value in the drop down is blank, and the user selects a balnk the debugger fires.
    Is there a way around this? I think that the drop down needs to ignore blank cells in the range, or that the macro diables when a blank value is selected.


    Thank you very much.

    marcoAUA.
    Last edited by MarcoAUA; 03-10-2012 at 12:34 AM.

  12. #12
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    The cell link is to have an easy access to the combobox value to use it in a formula. The combobox allways return the rank of the selected value, not the value itself although you see its text. With this value in cell C1, the formula in cell D1 gets the text value to be searched in the macro.
    To get around a value which can't be found, I added an IF statement. It makes sure the Find has found something in order to set the print range and show the page to print. Otherwise, it skips this part of program and ends the sub.
    Regards
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Perfect!

    I guess it is not possible that the drop down does not show blank entries?

    THNX a bundle for the help.

  14. #14
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Each value selected in dropdown fires different macro

    the dropbox shows you what is in the source range. You have to manage having no blank entries in that range.
    good luck

  15. #15
    Registered User
    Join Date
    01-10-2012
    Location
    Aruba
    MS-Off Ver
    Excel 2007
    Posts
    48

    Re: Each value selected in dropdown fires different macro

    Thank you so much.

    I'll be posting more threads, since the project is not ready yet.
    In the mean time, this one is SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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