+ Reply to Thread
Results 1 to 21 of 21

VBA to Define Names for columns

  1. #1
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    VBA to Define Names for columns

    I want to be able to define names for multiple columns by inserting the formula "=INDEX(Table,,"Variable") in the Refers to box in the New Name dialogue box.
    I used the macro recorder to get part of the way

    'Range("Table1[[#Headers],[_fd_id]]").Select
    'ActiveWorkbook.Names.Add Name:="_fd_id", RefersToR1C1:="=INDEX(Table,,1)"
    'ActiveWorkbook.Names("_fd_id").Comment = ""

    I then wrote the code below

    Sub DefineNameForColumns()

    FinalCol = Cells(1, Columns.Count).End(xlToLeft).Column

    For i = 1 To FinalCol


    Cells(2, i).Select
    ActiveWorkbook.Names.Add Name:=Cells(2, i).Value, RefersToR1C1:="=INDEX(Table,,i)"

    'ActiveWorkbook.Names("_fd_id").Comment = ""

    Next i

    How do I change the code to get the variable "i" expressed in "=INDEX(Table,,i)"

    Thanks

    Steve Excel 2013
    Last edited by [email protected]; 03-29-2015 at 11:06 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    Thanks for your help - works a treat

    Steve

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  5. #5
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    Sorry I'm new at this. Can I ask one more thing

    where the code reads RefersToR1C1:="=INDEX(Table,," & i & ")"

    I have been trying to replace the hard code "Table" with the active sheet name (as this is the same as the table name)
    with
    RefersToR1C1:="=INDEX(" & ActiveSheet.Name & ",," & i & ")"
    but get a 1004 error

    Thanks

    Steve

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    What is the Activesheet name?

  7. #7
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    In this example

    Cells(2, i).Select

    ActiveWorkbook.Names.Add Name:=Cells(2, i).Value, RefersToR1C1:="=INDEX(Term_1_Requests,," & i & ")"

    "Term_1_Requests" is the name of the table - I also use exactly the same name for the worksheet.

    I was hoping to find out how one might insert the worksheet name (which is the active worksheet) where the table name ("Term_1_Requests") currently is. Then I could use the macro on other sheets without having to manually enter the table name. I hope i have made this clearer

    Thanks

    Steve

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    Can you post a small sample workbook? Not sure how your worksheets and data are structured so it will help to put the code into context.

  9. #9
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Post Re: VBA to Define Names for columns

    FOR EXCEL FORUM.xlsx
    I attached a spreadsheet - hopefully this will make things clearer

    Steve

    At the risk of stating the very obvious a quick look in the name manager should help make clearer
    Last edited by [email protected]; 03-26-2015 at 09:51 AM.

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    OK, now I can see what the worksheet and table look like, I have to ask, why do you need to create all those Named Ranges?

    As it's a Structured Table, you have a number of built in references, so you don't really need to try to create Dynamic Named Ranges

    Please Login or Register  to view this content.
    I could, for example, create a Named Range, nrTMS:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Then I can refer to the Dynamic Named Range, nrTMS, which Excel will manage for me:

    Please Login or Register  to view this content.

    So, if you really want to create your own named ranges, you could build on that approach ... or you could not bother re-inventing the wheel and just use the gifts that Excel provides.


    Regards, TMS

  11. #11
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    I use the named ranges to generate INDEX & MATCH formulas to display information from one row of Term 1 Requests in a dashboard
    First I generate the following formula

    =INDEX(,MATCH(PivotTableValue,surname,0))

    I can choose the name of the person I want information on by using a slicer that generates PivotTableValue

    Thereafter I copy the formula above to other cells and only need to add the array part of the INDEX function.
    After I click in to where the array goes I get a dropdown or autofill box which lets me find the named range I want quickly

    =INDEX(first_name,MATCH(PivotTableValue,surname,0))
    Capture for excel forum.PNG

    I have attached full file to illustrate how I have used the named ranges.I have a number of similar projects and the ability to use the macro without having to manually change the table name every time would be useful

    Roster request DASHBOARD for forum.xlsm

    Hope that all makes sense

    Steve

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    This:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    could be, for example:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    And this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    could be this:
    Formula: copy to clipboard
    Please Login or Register  to view this content.



    Regards, TMS

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    You can also use the Structured Table references in Conditional Formatting. For example, you could use
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    applied to:
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    to highlight duplicate surnames in column H.

    Bit messy, of course, but it works well.

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    Or, strictly:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    I see where you are coming from. However given that the macro that you helped me to complete above is so quick and easy to use I then get the advantage of being able to use the dropdown (as illustrated in the picture above) to fill the array part of the INDEX function. That is not available using the structured table references (as far as I can tell). When I am adding 30 - 40 Index functions to a page that speeds things up for me

    Any thoughts on how I would go about coding the macro to use the sheet name for the array part of the INDEX formula

    i.e. RefersToR1C1:="=INDEX(ACTIVESHEET.NAME,," & i & ")"

    Thanks

    Steve

  16. #16
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    I would expect it to be something like:

    Please Login or Register  to view this content.

    In testing, I have a table, Table1, on a worksheet, Table1, which is the ActiveSheet. Hence ActiveSheet.Name is Table1.

    Both Name additions give exactly the same result for me.


    Regards, TMS

  17. #17
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    figured it out

    i.e. RefersToR1C1:="=INDEX(" & ACTIVESHEET.NAME & ",," & i & ")"

    Thanks again for taking the time to help out

    Steve

  18. #18
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    Good ... But how is that different to the code I posted two hours previously?

  19. #19
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    And how is it different to what you had in post #5?

  20. #20
    Registered User
    Join Date
    04-11-2013
    Location
    Perth WA
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA to Define Names for columns

    Sorry
    I did not realise the thread had moved onto a second page so missed your solution. As you point out I had almost solved it at post #5. The sheet I was using to test that had an column name that is not allowed as a defined name but as the 1004 error box is not very specific (and because I am a little slow with this stuff) it took me some time to figure it out

    Thanks again for all you time and effort - I have learned more than I expected to

    Steve

  21. #21
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,424

    Re: VBA to Define Names for columns

    You're welcome.

+ 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] define names
    By jwintsch in forum Excel General
    Replies: 2
    Last Post: 03-19-2013, 05:41 PM
  2. define names
    By wetrain17 in forum Excel General
    Replies: 1
    Last Post: 09-28-2011, 11:42 AM
  3. Define names in VBA
    By SpringLily in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-09-2011, 11:27 AM
  4. Define Names
    By aci1984 in forum Excel General
    Replies: 2
    Last Post: 02-06-2007, 03:08 PM
  5. alternate UI for Define Names ??
    By jmg092548 in forum Excel General
    Replies: 2
    Last Post: 08-11-2005, 08:32 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