+ Reply to Thread
Results 1 to 15 of 15

VBA to copy Named Ranges

  1. #1
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Question VBA to copy Named Ranges

    Hi. I need some help simplifying some VBA. I am including a sample spreadsheet.

    I have 7 separate named ranges that are all formatted the same, with some cells protected and some cells merged.

    I want to be able to copy the data (values only) from one of the named ranges into any (or all) of the named ranges without having to write a separate sub for every combination. Can this be done?

    For instance, I want the user to be able to click on a button below the area to be copied and they be asked where to copy it, giving check boxes, or some other way to choose where to put the copied data.

    I used the following code to copy one range to another specific range, but I would like to take that further if possible. Any help is appreciated.

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

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    I wouldn't do this with a whole bunch of buttons, I would do it with a user form. See attached, click the button in the upper left corner.

    Also, I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    Also you should get rid of merged cells. Nothing but trouble and your sheet can be formatted to look the same without them.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    I wouldn't do this with a whole bunch of buttons, I would do it with a user form. See attached, click the button in the upper left corner.

    Also, I strongly recommend to everyone that they use Option Explicit and declare variables. Doing so prevents a lot of bugs and runtime errors.

    Also you should get rid of merged cells. Nothing but trouble and your sheet can be formatted to look the same without them.
    Thank you! I am a novice, but this would be so much cleaner. I will use a userform instead.

    Is it possible to be able to select more than one Tier to copy (or clear) to at a time? As written, I can only choose 1 at a time.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    You can't copy from more than one tier at a time. Do you mean copy from one tier to multiple other tiers...?

    For clearing, the option buttons can be converted to checkboxes, and the code would have to be changed accordingly.

  5. #5
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    You can't copy from more than one tier at a time. Do you mean copy from one tier to multiple other tiers...?

    For clearing, the option buttons can be converted to checkboxes, and the code would have to be changed accordingly.
    Yes, I meant copy from 1 tier to multiple tiers. Thanks for your help.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    I'll try to find a few minutes to make those updates.

  7. #7
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    I'll try to find a few minutes to make those updates.
    Wow. Thank you. I really appreciate it.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    Here you go!

    It prevents you from copying a tier back onto itself. It will not notify you if you have no checkboxes selected; it just won't do anything.
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    Here you go!

    It prevents you from copying a tier back onto itself. It will not notify you if you have no checkboxes selected; it just won't do anything.
    Thank you so much! This is great. It's exactly what I need.

  10. #10
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    Here you go!

    It prevents you from copying a tier back onto itself. It will not notify you if you have no checkboxes selected; it just won't do anything.
    Hi again.

    I apologize for bothering you again, but I realized after I marked the string as solved that I had made a mistake in my sample that I provided to you. I was hoping that I could figure out the code enough to correct it myself, but I was unsuccessful.

    In my original sample I did not have enough lines to represent my actual spreadsheet and I had 7 named ranges. I should have had 14 named ranges. 7 for the copy function and 7 different ones for the clear function.

    I was hoping that I could just change the names of the ranges in the code, but I cannot find where it references the named ranges.

    I have scoured the code you provided, but I must be missing something.

    Can you help me again?
    Attached Files Attached Files

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    It determines the named ranges dynamically based on the names of the checkbox that is checked. You conveniently named your ranges using numbers 1-7. I'll look at what you have.

  12. #12
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    7 for the copy function and 7 different ones for the clear function.
    which ones are which?

  13. #13
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    which ones are which?
    Sorry. The ones that end in "ALL" go with the Clear set. The ones with just the Tier_# go with the Copy set.

  14. #14
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: VBA to copy Named Ranges

    Here's an update. The change is in the Clear sub where the name of the tier is determined based on the checkbox. I also added some comments to help clarify what the code does.
    Attached Files Attached Files

  15. #15
    Forum Contributor
    Join Date
    11-08-2017
    Location
    Murfreesboro, TN
    MS-Off Ver
    MS 365
    Posts
    168

    Re: VBA to copy Named Ranges

    Quote Originally Posted by 6StringJazzer View Post
    Here's an update. The change is in the Clear sub where the name of the tier is determined based on the checkbox. I also added some comments to help clarify what the code does.
    Thanks again! That does exactly what I need. I really appreciate the extra explanation of the code.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy Named Ranges
    By brentjohn in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-20-2019, 07:21 PM
  2. how to drag or copy NAMED RANGES
    By geniusufo007 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-27-2017, 03:25 AM
  3. Copy values of all named range in wb1 to identically named ranges in wb2
    By JAMIAM in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-24-2016, 06:58 PM
  4. copy named ranges
    By ammartino44 in forum Excel General
    Replies: 1
    Last Post: 05-06-2015, 08:15 PM
  5. [SOLVED] Copy pasting two named ranges
    By Nils88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-13-2013, 11:32 PM
  6. Named Ranges copy themselves!
    By shaundbrown in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-25-2010, 01:06 PM
  7. Copy data in named ranges to a newer version of the same template to identical ranges
    By handstand in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-21-2006, 10:51 AM

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