+ Reply to Thread
Results 1 to 9 of 9

VBA to create specific named ranges and repeating process (Loop?) across columns

Hybrid View

vinothj86 VBA to create specific named... 08-24-2012, 02:12 PM
jaslake Re: VBA to create specific... 08-24-2012, 02:50 PM
jaslake Re: VBA to create specific... 08-24-2012, 03:46 PM
vinothj86 Re: VBA to create specific... 08-25-2012, 12:57 PM
jaslake Re: VBA to create specific... 08-25-2012, 01:09 PM
jaslake Re: VBA to create specific... 08-25-2012, 02:04 PM
vinothj86 Re: VBA to create specific... 08-25-2012, 01:16 PM
jaslake Re: VBA to create specific... 08-25-2012, 01:20 PM
vinothj86 Re: VBA to create specific... 08-30-2012, 05:01 AM
  1. #1
    Registered User
    Join Date
    05-23-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    4

    VBA to create specific named ranges and repeating process (Loop?) across columns

    Dear VBA pros,

    I am quite new to VBA but love how it simplifies tasks.

    I have written a basic code (in Module 2) to help create named ranges based on the top row (highlighted cells). It doesnt seem to work and I was wondering if someone could help? I have attached the Excel file.

    For example, the first named range should be "Revenues" and the cells that should be designated are C3 to F6. then I would like to repeat the process to the next set of data, the named range being "Selling Expenses", to cover cells G2 to J6. How can I loop this process till it reaches an empty cell?

    Thanks very much.

    V
    Attached Files Attached Files

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi vinothj86
    The reason your code errors is that it's missing a Colon...see .jpg attached.
    semi.jpg
    I know this doesn't answer your broader question (which I'll look at) but thought you might wish to know.

    Are you ALWAYS going to have 4 years and 4 products?
    Last edited by jaslake; 08-24-2012 at 02:54 PM.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi vinothj86

    Try this code to name your ranges. Change your Column Heading for Other Expenses to Other_Expenses (no blanks).
    Sub Example()
        Dim vHdrs()
        Dim lMatch As Long
        Dim lngHdr As Long
        Dim rng As Range
        Const C_Sheet = "Database"
        Const C_Row = 1
        On Error GoTo ExitPoint
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
        vHdrs = Array("Revenues", "Sells", "Comms", "Travs", _
                "Fairs", "Legal", "Trady", "Sport", "Misc", "Other_Expenses", "EBIT")
        For lngHdr = LBound(vHdrs) To UBound(vHdrs) Step 1
            lMatch = Application.Match(vHdrs(lngHdr), Sheets(C_Sheet).Rows(C_Row), 0)
            If IsNumeric(lMatch) Then
                Set rng = Cells(1, lMatch).Offset(2, 0).Resize(Range("B1"), Range("B2"))
                ActiveWorkbook.Names.Add Name:=vHdrs(lngHdr), refersto:=rng
            End If
        Next lngHdr
    ExitPoint:
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    05-23-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi jaslake! Thanks so much for your help.

    To answer your first question, the number of products and years will change and that's why I tried to make it dynamic in my basic code. My idea with this is that the no of years and products will influence how far right and down the macro will go before it loops to do the next named range. It will eventually stop when it reaches an empty cell on the far right.

    The other issue is that the headers such as revenues, selling expenses etc will change from time to time but in your code it appears to be defined (within the array function). Is there a way to make this dynamic? Or am I just reading the code wrongly?

    I've just tested your code but it seems to stop after doing "revenues" correctly (i have changed headers with gaps to underscores).

    Thanks again and I look forward to your reply.

    Vinoth

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi Vinoth

    I don't understand this as it was tested and ran fine for me on your sample file
    I've just tested your code but it seems to stop after doing "revenues" correctly
    Did you use a different file in your test?

    I'll look at this and get back to you
    the headers such as revenues, selling expenses etc will change from time to time ...Is there a way to make this dynamic?

  6. #6
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi Vinoth

    This code is in the attached and accommodates this
    The other issue is that the headers such as revenues, selling expenses etc will change from time to time but in your code it appears to be defined (within the array function). Is there a way to make this dynamic?
    ' Adapted from http://stackoverflow.com/questions/6775165/collect-all-names-in-a-column-and-put-them-in-an-array-in-excel
    Option Explicit
    Sub Create_Named_Ranges()
    
        Dim i As Long
        Dim LC As Long
        Dim HeaderList As Range
        Dim cUnique As New Collection
        Dim Header() As String
        Dim lMatch As Long
        Dim lngHdr As Long
        Dim rng As Range
    
        With Sheets("Database")
            LC = .Cells.Find(What:="*", After:=[A1], _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlPrevious).Column
            Set HeaderList = .Range(.Cells(1, 3), .Cells(1, LC))
            'Loop over every column and add the value to the collection (with unique key)
            For i = 1 To HeaderList.Columns.Count
                On Error Resume Next
                If Not IsEmpty(HeaderList(1, i)) Then
                    cUnique.Add HeaderList(1, i), CStr(HeaderList(1, i))
                End If
            Next i
            'Store back the value from the collection to an array
            ReDim Header(1 To cUnique.Count, 1 To 1)
            For i = 1 To cUnique.Count
                Header(i, 1) = cUnique(i)
            Next i
        End With
        For lngHdr = LBound(Header) To UBound(Header) Step 1
            lMatch = Application.Match(Header(lngHdr, 1), Sheets("Database").Rows(1), 0)
            If IsNumeric(lMatch) Then
                Set rng = Cells(1, lMatch).Offset(2, 0).Resize(Range("B1"), Range("B2"))
                ActiveWorkbook.Names.Add Name:=Header(lngHdr, 1), refersto:=rng
            End If
        Next lngHdr
    End Sub
    Let me know of issues.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    05-23-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    I did try it on a different file. Does that make it not work?

    Thanks again jaslake. Much appreciated.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Hi Vinoth

    No that should NOT make it not work but there's something different about the file that IS making it not work.
    Are you able to attach YOUR test file?

  9. #9
    Registered User
    Join Date
    05-23-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: VBA to create specific named ranges and repeating process (Loop?) across columns

    Sorry for the late reply jaslake! Will test it soon and revert. All your help is much appreciated!

+ 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