+ Reply to Thread
Results 1 to 10 of 10

How to make my code work with table column names

  1. #1
    Registered User
    Join Date
    01-04-2013
    Location
    atlanta ga
    MS-Off Ver
    Excel 2007
    Posts
    36

    How to make my code work with table column names

    So I inherited this workbook from a coworker who knew waaaaaay more about VBA than I do. I just need to be able to add columns to the "compliance tracker" tab while still having my code work as a table since I think that is probably easiest to code for. My workbook is attached.

    I am more than happy to take any suggestions to make this easier. I appreciate all your help!!!

    Thanks!
    Dave


    Compliance Tracker - Corporate Draft - updating v2.xlsm

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to make my code work with table column names

    Hi Dave,

    I'm not sure you need to do anything. An Excel Table range will automatically adjust as you add a new column immediately to the right of column U. That's what Tables do.

    That said I note that Table 1 is defined as A9:U1048576 which suggests that at some time the very last row has been included in the table so it would be worth tidying that up unless you really do want 1 million plus rows. Convert it to a range and then recreate it with the Insert table option again.

    Personally I rarely use the Table functionality. I just don't like the syntax of formulae within a table and find them more difficult to read. I generally use dynamic range names with regular ranges whenever I need to ensure that additions to a range are automatically included in the range definition.

    I also note that there seems to be an awful lot of repetition in the GenerateEmails procedure.
    The only lines that seems different in all the Case statements are ones beginning
    toemail = "GetMail....etc
    subemail = "CorpTaxID:......." etc.

    The rest could be set once.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    01-04-2013
    Location
    atlanta ga
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: How to make my code work with table column names

    Quote Originally Posted by Richard Buttrey View Post
    Hi Dave,

    I'm not sure you need to do anything. An Excel Table range will automatically adjust as you add a new column immediately to the right of column U. That's what Tables do.

    That said I note that Table 1 is defined as A9:U1048576 which suggests that at some time the very last row has been included in the table so it would be worth tidying that up unless you really do want 1 million plus rows. Convert it to a range and then recreate it with the Insert table option again.

    Personally I rarely use the Table functionality. I just don't like the syntax of formulae within a table and find them more difficult to read. I generally use dynamic range names with regular ranges whenever I need to ensure that additions to a range are automatically included in the range definition.

    I also note that there seems to be an awful lot of repetition in the GenerateEmails procedure.
    The only lines that seems different in all the Case statements are ones beginning
    toemail = "GetMail....etc
    subemail = "CorpTaxID:......." etc.

    The rest could be set once.
    Hi Richard

    My problem is that when I add columns, my generate email macro doesn't work. I don't even come close to knowing enough about VBA how to fix that.

    Thanks!

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to make my code work with table column names

    Hi,

    Once you have amended the table size as Richard suggested, you might simply loop through the table rows and refer to the columns by name like this
    Please Login or Register  to view this content.
    for all the other column references you would use the same syntax and simply replace the blue section with the relevant column name.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to make my code work with table column names

    Would you clarify what you mean by 'doesn't work'.

    The macro simply sends an email with something like
    Coprtax ID: C1000d204 Entity Name: Entity 5 is in Prep.
    in the body of the email.

    I can't see how adding additional columns is of any relevance.

    However doing an End Down on any of the empty columns arrives at row 1,048,555. This is the case with column R and in the macro column R is used to determine the last row and hence the number of iterations the macro must process. If column R only contains "Not Started" or blank then the macro will take a long time to process all 1 million + rows which may be what you mean by 'not working'. The fact that you may also have added columns I don't believe is the problem here.

    I suggest that you select all rows from row 199 down to the bottom and delete them all, then try the macro.

    However clarify what you mean by doesn't work if this doesn't change anything.

  6. #6
    Registered User
    Join Date
    01-04-2013
    Location
    atlanta ga
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: How to make my code work with table column names

    Quote Originally Posted by Richard Buttrey View Post
    Would you clarify what you mean by 'doesn't work'.

    The macro simply sends an email with something like
    Coprtax ID: C1000d204 Entity Name: Entity 5 is in Prep.
    in the body of the email.

    I can't see how adding additional columns is of any relevance.

    However doing an End Down on any of the empty columns arrives at row 1,048,555. This is the case with column R and in the macro column R is used to determine the last row and hence the number of iterations the macro must process. If column R only contains "Not Started" or blank then the macro will take a long time to process all 1 million + rows which may be what you mean by 'not working'. The fact that you may also have added columns I don't believe is the problem here.

    I suggest that you select all rows from row 199 down to the bottom and delete them all, then try the macro.

    However clarify what you mean by doesn't work if this doesn't change anything.
    When i add an new columns before column R and hit generate email, say when I have switched a status to "In-prep", it just says process complete and does not actually generate an email. I think that's because the code is made so it looks at a specific column (i am not good at VBA at all). thats the problem I am having.

  7. #7
    Registered User
    Join Date
    01-04-2013
    Location
    atlanta ga
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: How to make my code work with table column names

    Quote Originally Posted by xlnitwit View Post
    Hi,

    Once you have amended the table size as Richard suggested, you might simply loop through the table rows and refer to the columns by name like this
    Please Login or Register  to view this content.
    for all the other column references you would use the same syntax and simply replace the blue section with the relevant column name.
    is there a way i could have cells with those names in them, then link to those cells within the blue text you put so that i could easily change the names in the cells if i want to change the column that is being looked at?

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to make my code work with table column names

    Quote Originally Posted by dangerdavedsp View Post
    When i add an new columns before column R and hit generate email, say when I have switched a status to "In-prep", it just says process complete and does not actually generate an email. I think that's because the code is made so it looks at a specific column (i am not good at VBA at all). thats the problem I am having.
    Ahh!

    That's clearer now. I had assumed you were adding columns to the right of the last column U. Adding columns before column R of course shifts all columns across and what was column R now becomes column S - assuming you just add a single column. Unfortunately the reference to column R is hard coded in the macro in the line
    Please Login or Register  to view this content.
    and
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    and so the macro never finds any of the Case statements.

    All of which is an excellent example of why it's never good practice to hard code sheet names or cell and range addresses in the macro. You should always use range names.

    So first define the name 'Status' as R8

    Now change the first instruction in the macro to
    Please Login or Register  to view this content.
    and the second instruction to
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    01-04-2013
    Location
    atlanta ga
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: How to make my code work with table column names

    Quote Originally Posted by Richard Buttrey View Post
    Ahh!

    That's clearer now. I had assumed you were adding columns to the right of the last column U. Adding columns before column R of course shifts all columns across and what was column R now becomes column S - assuming you just add a single column. Unfortunately the reference to column R is hard coded in the macro in the line
    Please Login or Register  to view this content.
    and
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    and so the macro never finds any of the Case statements.

    All of which is an excellent example of why it's never good practice to hard code sheet names or cell and range addresses in the macro. You should always use range names.

    So first define the name 'Status' as R8

    Now change the first instruction in the macro to
    Please Login or Register  to view this content.
    and the second instruction to
    Please Login or Register  to view this content.
    Thank you so much Richard! I will try this code tonight!

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: How to make my code work with table column names

    Quote Originally Posted by dangerdavedsp View Post
    is there a way i could have cells with those names in them, then link to those cells within the blue text you put so that i could easily change the names in the cells if i want to change the column that is being looked at?
    Of course- it would look like this
    Please Login or Register  to view this content.
    for example.

+ 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. need vba code to validate the names from names of group in column a:a
    By johnlara in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-07-2016, 11:28 AM
  2. Need help to simplify macro and make it work on same sheet again (with names?)
    By wkarotten in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-04-2014, 05:24 AM
  3. Show query column names behind pivot table field names
    By gbritton in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 11-18-2013, 03:17 PM
  4. Replies: 1
    Last Post: 08-14-2013, 12:01 PM
  5. [SOLVED] How to make a table that count by the names
    By solloman in forum Excel General
    Replies: 7
    Last Post: 05-12-2012, 03:48 PM
  6. how to make code work
    By Oeysbrei in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2012, 06:22 AM
  7. to make a pivot table to work with an adjoining table
    By byronova in forum Excel General
    Replies: 2
    Last Post: 09-06-2009, 10:47 AM

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