+ Reply to Thread
Results 1 to 6 of 6

Change code so that Dynamic ranges include sheet name

  1. #1
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Change code so that Dynamic ranges include sheet name

    Hello, I am using the code below to create dynamic named ranges for a table of data. This code works wonderfully if there is only one data table in a workbook, however, if you use it on more than one data table it creates issues. The code creates a two named ranges ,"lrow" and "lcol" to count the last row and column of the worksheet. What I would like it to do is create those named ranges with the worksheet name appended to the front. So if your data table was on a worksheet named "data" the name of the two ranges would be "datalrow" and "datalcol". Modifying it in this way would allow you to use it on as many data tables you need in the same workbook.

    Please Login or Register  to view this content.
    Thanks for any help you can provide!
    Clayton Grove

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,372

    Re: Change code so that Dynamic ranges include sheet name

    Hi dcgrove,

    I recorded a macro and created two names. The first was a Workbook name and the second was a WorkSheet name. Then I stopped recording and looked at the recorded macro. This gave me the answer you are looking for.

    When you code
    Please Login or Register  to view this content.
    it adds a workbook name. If you want to add a worksheet name you need to use
    Please Login or Register  to view this content.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Change code so that Dynamic ranges include sheet name

    Marvin, thanks for the suggestion! The worksheet name will not always be "data". I used that name as an example. The macro will need to be able to determine the name of the worksheet it is being run on and append that name to the front of "lcol" or "lrow". Unfortunately, that is not something I know how to write.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,372

    Re: Change code so that Dynamic ranges include sheet name

    Hi,
    Simply grab the sheet name in a string like
    Please Login or Register  to view this content.
    Then use that in the code like
    Please Login or Register  to view this content.
    does that help?

    If it isn't the ActiveSheet and you know it is the 4th sheet tab you can use
    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Change code so that Dynamic ranges include sheet name

    Ok, I have been playing around and have been able to get the "lcol" and "lrow" names to be named with the sheet name appended, as well as the rest of the named ranges. However, I am not sure how to edit the lines below so that it uses the sheetname&lcol or sheetname&lrow ranges in the formulas. Below is my updated code and an example workbook that I ran the code on so you can see the named ranges it is creating.


    =Sheet1!$A$2:INDEX(Sheet1!$A:$A,shtname & lrow)

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

  6. #6
    Forum Contributor
    Join Date
    02-16-2008
    Location
    Mansfield, TX
    Posts
    324

    Re: Change code so that Dynamic ranges include sheet name

    Ok, I fiddled with this some more and figured out how to accomplish what I need. This is the code I am using now.

    Please Login or Register  to view this content.
    Now I just need to figure out how to determine if the sheet name contains spaces so I can use the replace function to replace the spaces with and underscore.
    Last edited by dcgrove; 06-02-2011 at 03:56 PM.

+ 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