+ Reply to Thread
Results 1 to 35 of 35

How do you combine code that requires different Sub Worksheet actions?

  1. #1
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    How do you combine code that requires different Sub Worksheet actions?

    Hello again all. I have 3 different Sub actions I need to combine. I am using:

    Please Login or Register  to view this content.
    in ThisWorkbook. This allows me to select the next blank row in the designated sheet which is populated with various dropdowns and cell validations using the following code in the specific sheet:

    Please Login or Register  to view this content.
    I am trying to add in a pop-up calendar from the forum using the following code:

    Please Login or Register  to view this content.
    The problem is I can not merge the calendar and sheet code. If I add in the calendar code under the
    Please Login or Register  to view this content.
    , it does not work. If I change the
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    , the original sheet code does not work. I tried to add the calendar code in a seperate block after the original sheet code, but that did not work either.

    I also tried variations in ThisWorkbook.

    Ideally, I want the calendar to pop-up in every date field on several sheets so ThisWorkbook may be the best place to add it if possible.

    Any help would be appreciated!!!

    Thanks,
    Andrew
    Last edited by drewship; 09-29-2010 at 11:13 AM.

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    Use the Workbook_SheetSelectionChange event in the ThisWorkbook module. (not really clear on why you were trying to merge the other codes together though?)
    Remember what the dormouse said
    Feed your head

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,481

    Re: How do you combine code that requires different Sub Worksheet actions?

    This could probably be cut down.

    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.
    You don't need all that other code just to select a cell, except for the enable events if you don't want the Selection_Change to activate right away.
    Last edited by davesexcel; 09-24-2010 at 08:43 AM.

  4. #4
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks romperstomper. I could not get the the different code blocks to work while seperated so I thought I might be able to merge them. I will try your recommendation. Thanks again.

    Andrew

  5. #5
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks davesexcel. I tried your code but it does not allow me to select the next column to input data, but instead forces me to select the next blank cell in column A. I need to be able to select/enter on each cell to the right...not down. What do I need to add to fix it?

    romperstomper, I made the change you recommended and moved code around in various configurations but If you could clarify where/how the various code blocks should be implemented, it would be very helpful.

    Thanks,
    Andrew

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    I can't do that unless I know what the problem is. They are separate events after all.

  7. #7
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    romperstomper, the workbook contains several sheets and I want to add a popup calendar to every column that requires a date. Please see attached workbook that has my currently working code...but no calendar code. If you can help me understand how to include either the previously posted calendar code or something new, it would be greatly appreciated.

    Thanks,
    Andrew
    Attached Files Attached Files

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    I can, but it will probably not be before Monday as it's late Friday afternoon now...

  9. #9
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    No worries...I will play with it too but since I am not as familiar as you are with the interaction of VBA, I can hope to get lucky...


    Thanks again!!

    Andrew

  10. #10
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Ok...started new from the previous file I attached and tried (again) to add the calendar code blocks to the end of the sheet code and it mostly works. It will not allow me to select the current day or any previous day...will keep trying...

    Andrew
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Ok...after hours of testing various forum solutions, attached is my latest attempt which seems to work on a single sheet. It allows selections in multiple columns as well as previous/current dates. I still need to modify it to have calendar popups on multiple sheets and if someone can help me shorted the following code, it would be greatly appreciated:

    Please Login or Register  to view this content.
    Thanks!!
    Andrew
    Attached Files Attached Files

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    You can place this in the ThisWorkbook module to handle all the Calendar popups on all sheets. The way this works is it examines the numberformat of each cell as you select them. I've already identified 3 different DATE numberformats in your sheet and added code to popup for those date formats. If you have any other date formats, just add them as needed.

    Please Login or Register  to view this content.


    Be sure to remove all the code in the sheet modules that do calendar popups.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    Here's another version that uses the common string "d/y" which is in all your date formats so far and it seems to work, too.

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks JBeaucaire. I am already using

    Please Login or Register  to view this content.
    in the ThisWorkbook module and when I changed this to test other calendar code, the following code stopped firing.

    Please Login or Register  to view this content.
    Without this code, the various other validation functions coded in the worksheets do not work. I am done for the day but will certainly see if I can work in the code you provided tomorrow. I may have had too many changes already in place which corrupted my initial efforts.

    Andrew

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    1) I don't see the merit in opening sheets and making selections, does that accomplish something specific for you? In general, selecting is something you edit out of VBA.

    2) The code you're using...with all these sheet-event macros, you might want to add this to your wb_open macros

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks JBeaucaire. I was using that code in the ThisWorkbook module since it seemed I needed it to allow the dropdowns to be populated by the sheet code. I removed it and used your code and the dropdowns are still populating, but the dates are not. I have attached my latest workbook

    Andrew
    Attached Files Attached Files
    Last edited by drewship; 09-28-2010 at 02:17 PM.

  17. #17
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Ok...reformatted all the date columns on both sheets and now the dates seem to be working on both sheets!!!

    Another problem I noticed is if you enter text in a column that is not validated in any way, the cell automatically gets re-selected instead of moving to the next cell, which in my case should be the cell in the next column. Any thoughts why this is occuring and how to correct it?

    I also saw where you created a Modeless calendar and provided for the Esc key to close it in post: http://www.excelforum.com/excel-prog...ast-dates.html and I would like to do the same thing if possible. Been trying to work in your code from that post but no luck so far.

    Thanks again,
    Andrew
    Last edited by drewship; 09-28-2010 at 08:58 AM.

  18. #18
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    The modeless calendar uses a hidden control that is hidden on the form, but hidden behind the calendar itself. Toggle through the controls and you'll see one that appears to be behind the calendar, check the properties on that control.

    Give me the link to the post where you got that and I can help, I remember doing it just not in my brain exactly how.

  19. #19
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    Use a button and set its Cancel property to True. Then the button click code just needs to hide the form.

  20. #20
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Here is the link:


    http://www.excelforum.com/excel-prog...ast-dates.html


    I looked behind the calendar and tried to incorporate the code which I have been unsuccessful to this point.

    romperstomper, I added this code from the link above to my sheet thinking this would close the calendar when Esc was pressed. Should this code be something else or is this not the manner to use Esc to close the calendar?

    Please Login or Register  to view this content.


    Thanks all,
    Andrew

  21. #21
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    Do you have a button called cmdClose and is its Cancel property set to True? Both are necessary.

  22. #22
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    romperstomper, I thought I had the necessary code but apparently I do not. Will look through the link above and the forum to see how the Esc button should be coded...

  23. #23
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    In case it's not clear, this is what is required along with the code you already found:
    Attached Images Attached Images

  24. #24
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  25. #25
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks!! That is the piece I was missing...I couldn't find the button anywhere because I was looking at the calendar code, not at the calendar properties...

    Please see the attached workbook which illistrates the below issues.

    I have noticed 2 additional problems. First is if you enter text in a column that is not validated in any way, the cell automatically gets re-selected instead of moving to the next cell, which in my case should be the cell in the next column. Enter something in the POC columns to see what I mean.

    The other problem is I need a 3 color validation in column I but conditional formatting will not allow referential values in the formula to calculate the 3 colors. I am using :

    Please Login or Register  to view this content.
    in an attempt to achieve the same result but the cells stay uncolored no matter what date I enter.

    Any thoughts why this is occuring and how to correct it?
    Attached Files Attached Files

  26. #26
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    Take out this line of code in your ws_change macro:
    Please Login or Register  to view this content.

  27. #27
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    I would rewrite the whole column I data validation like so:

    Please Login or Register  to view this content.
    Attached Files Attached Files

  28. #28
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Wow!!! I was deep in the forum looking up nested If variations...but this is so much better, and I can see how to modify this for additional formula/color combinations!!

    Thanks everyone for all your help!!

    Andrew

  29. #29
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Ok...sorry to open this thread again but I didn't get to test the code fully yesterday. I was getting:

    Error 91 Object Variable or with block variable not set.

    at line:

    Please Login or Register  to view this content.
    and thought I had mistyped something but I downloaded the file JBeaucaire provided with the changes and it is also giving the above error. This occurs on the OrderTracking sheet when a selection is made in any column. I don't see any with block issues but not sure what object variable is causing an error.

    Thanks, for looking at this again.

    Andrew
    Attached Files Attached Files

  30. #30
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    That line is missing an 'Is Nothing' - it should be:
    Please Login or Register  to view this content.

  31. #31
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    Change this:
    Please Login or Register  to view this content.
    ...to this:
    Please Login or Register  to view this content.

    ...or this:
    Please Login or Register  to view this content.

  32. #32
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: How do you combine code that requires different Sub Worksheet actions?

    FYI, your code really ought to be redone using loops in case more than one cell is changed at once. If you copy a row (or a few cells from row to row), it will cause an error.

  33. #33
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: How do you combine code that requires different Sub Worksheet actions?

    RS is right, something like this:
    Please Login or Register  to view this content.

  34. #34
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    Thanks!!. That did the trick. Code logic seems to elude me at times so I did not even think twice about how that line was written.

    Andrew

  35. #35
    Forum Contributor
    Join Date
    04-30-2009
    Location
    USA
    MS-Off Ver
    Excel 2016
    Posts
    496

    Re: How do you combine code that requires different Sub Worksheet actions?

    RS, I do not anticipate changing more than 1 row at a time but your point and JBs code are well taken since you never really know what may be attempted...


    Andrew

+ 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