Results 1 to 4 of 4

Iteration naming range names in particular worksheets

Threaded View

  1. #1
    Registered User
    Join Date
    01-17-2016
    Location
    NSW, Australia
    MS-Off Ver
    Office 2010
    Posts
    2

    Iteration naming range names in particular worksheets

    G'day y'all,

    I am trying to write some code which performs the following routines:
    1. Remove any current Range Names
    2. On certain worksheets, set some range names for an unknown number of rows (ie. they will change every month)

    My code is below. For some reason, the While . . . Wend routine will only use the last used row number for the worksheet "Week 1". The Do Until, while the With . . . End loop seems to change focus to each of the 5 worksheets, seems to always stop when y= the last row of the active worksheet. So, it seems the With Worksheets() isn't changing focus to each individual sheet and I end up with the correct range names, but only based on the number of active rows in the active sheet.

    Sub Week1()
    '
    ' Week1 Macro
    ' Select and name the range for Weeks 1-5
    '
    
    '   Declare Variables
        Dim ShtNum As String
        Dim x, y, z As Integer
        Dim nm As Name
    
    '   Remove all other Range Names in this Worksheet
        On Error Resume Next
        For Each nm In ActiveWorkbook.Names
            nm.Delete
        Next
    
    '   Set Variables for first part of the macro, which is to find the last non-empty row
        x = 1 ' column number to start count, this will count down in the referenced column to search for data (3 = C, 1 = A, 5 = E, etc.)
        y = 1 'row number ' row number to start on, if you have blank rows at the top you can start down lower like I did here
    
    '   Find the last non-empty row in Column A
        z = 1
        While z < 6
            With Worksheets("Week " & z)
                Do Until Cells(y + 1, x).Value = ""
                    y = y + 1
                Loop
            End With
    '   Set the Named Range for Week 1
            Worksheets("Week " & z).Range("A1:A" & y).Name = "Week" & z
            z = z + 1
            y = 1
            x = 1
        Wend
        On Error GoTo 0
    End Sub
    Other important points:
    • Excel 2010
    • There should be no other Range Names used in the Workbook
    • I want to use the Ranges in a summary sheet in SumIF() statements


    Thank you,

    Mitchies
    Last edited by Mitchies; 01-18-2016 at 05:17 PM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 3
    Last Post: 09-17-2014, 01:42 PM
  2. Copy Range Names To Other Worksheets In Workbook
    By The Skipper in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2013, 06:06 PM
  3. How to Scan All Worksheets in a Certain Range, Compile Lists of WS names
    By justinprime in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-12-2012, 02:09 AM
  4. Creating and naming multiple worksheets; copying contents to new worksheets
    By HeadfortheHills in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-14-2012, 08:49 AM
  5. Iteration for sheet names
    By Dalia in forum Excel General
    Replies: 3
    Last Post: 10-28-2006, 01:03 PM
  6. [SOLVED] not delete worksheets from names in a range
    By DARREN FONG in forum Excel General
    Replies: 3
    Last Post: 11-11-2005, 01:35 PM
  7. [SOLVED] deleting worksheets from names in a range
    By Jenn in forum Excel General
    Replies: 2
    Last Post: 08-22-2005, 07:05 PM

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