+ Reply to Thread
Results 1 to 21 of 21

Copy sheet based on Inputbox - data to be copied variable.

  1. #1
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Copy sheet based on Inputbox - data to be copied variable.

    I have many sheets named in the MMM YYYY format, and I want an Inputbox to ask what sheet they want copied.
    What I want the macro to do is take the sheet the user wants, copy it to the last sheet and rename it as MMM YYYY + 1 year. I do not want ALL rows to be copied though, only those that do not have a certain text string in a cell.

    Ie if the user types in Aug 2012 to be copied, Aug 2012 sheet is copied to the last sheet and is renamed Aug 2013. but I want only the rows of data to be copied that do NOT have the status of "NTU", "Declined", "Non-Renewed" or "Extended" in Colomn I17:I190. All other data in the sheet is to be copied.

    copytest.xls

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Give this a try:


    Please Login or Register  to view this content.
    Gary's Student

  3. #3
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Wow that works great, the only snag is the
    Please Login or Register  to view this content.
    is there any way this can be changed so that it clears the contents rather than deletes the entire row?

    I need it that way as I have an event macro that sorts the rows and it knackers my row alignment further down the sheet.

  4. #4
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    ignore me, I figured it out by replacing .delete with .clearcontents!!

    thank you so much!

  5. #5
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Oh before i go - is there code that can be put in that stops the sub if the user cancels the inputbox?

  6. #6
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Here is one way:

    Please Login or Register  to view this content.

  7. #7
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Absolutely brilliant. I googled that for ages and havent come up with that Cbool code.

    thanks again!

  8. #8
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    MY LAST POST HAS AN ERROR
    Instead use:

    Please Login or Register  to view this content.
    The CBOOL thing won't work in the normal case. We need to change shName to Variant.

  9. #9
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Strange it works for me! I will test your new code when I get to my pc tonight

  10. #10
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    tried both, both work for me.....?!

  11. #11
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Jakobshavn
    Ah i see now your original code doesnt work if you DONt hit cancel. Thanks for the update.

    Sorry to keep bugging you with this, but is there a way to enhance the code to say that if there is no sheet available to copy when the user is asked (for example if the user asks to copy Jul 2012 and no Jul 2012 sheet exists) that there is an error message and the sub is exited.

    Furthermore can the user be forced to enter in the format Mmm YYYY ?

  12. #12
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    The first part is easy. The hi-lighted block of code checks for a non-present source sheet:

    Please Login or Register  to view this content.
    Forcing a certain user response format is more difficult......................I'll have to think about it................

  13. #13
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Here is one method:

    Please Login or Register  to view this content.
    This needs more testing..............................

  14. #14
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Compile error Byref argument type mismatch

    related to

    If Not GoodName(shName) Then
    Last edited by nickmax1; 09-07-2012 at 09:23 AM.

  15. #15
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Noticed a problem with the

    Please Login or Register  to view this content.
    Instead of clearing the entire row how do I change so it clears the range A:K instead?

    Also my efforts of putting in a messagebox to say a "sheet has been requested that doesnt exist" has failed - how can this be implemented?

  16. #16
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Your Compiler is better than mine. Use:

    Public Function GoodName(s As Variant) As Boolean
    instead of the original line

  17. #17
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Brilliant that does the trick.

    Any chance Msgboxes could be used if there is a problem rather than just exiting sub straight away?

  18. #18
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    Now you are talking like a real software designer! We could create a new sub just for user error checking and put all the error messages in that sub.
    Last edited by Jakobshavn; 09-07-2012 at 10:08 AM. Reason: typo

  19. #19
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    hehe what a pain i am.....I am no software designer, just massively ****!!

    I am writing this for people who need to have their hands held at every turn when it comes to Excel so want it as clear as possible!

    as i mentioned before another issue is the

    Please Login or Register  to view this content.
    this needs to be changed to only the contents of A:K are cleared. as my formulas in colomns L:O and AH:AI are removed which i dont want.

  20. #20
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Copy sheet based on Inputbox - data to be copied variable.

    O.K.
    Here is the fix for A:K

    Please Login or Register  to view this content.
    Last edited by Jakobshavn; 09-07-2012 at 10:38 AM. Reason: typo

  21. #21
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Copy sheet based on Inputbox - data to be copied variable.

    wonderful it works!

    you are gonna hate me when you get to my next issue!!

    http://www.excelforum.com/excel-prog...n-formula.html

+ 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