+ Reply to Thread
Results 1 to 14 of 14

VBA to insert missing columns based on a list

  1. #1
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Exclamation VBA to insert missing columns based on a list

    Hi,

    I am struggling with a macro that is supposed to compare 10 columns of a sheet to a list of column headers, and add the columns that are missing (with just blank values).

    The problem is that the list is dynamic, with different column headers depending on which "company" is chosen in a cell ("T5"; I am aware that the cell moves around in the sheet when columns are inserted, but in the real workbook the company dropdown is located in a different sheet).

    I have adapted the code from here https://stackoverflow.com/questions/...ed-from-a-list, but cannot seem to get the dynamic element to work.

    Here is the code I have used so far:

    Please Login or Register  to view this content.
    The problem seems to be the
    Please Login or Register  to view this content.
    part, where I get a run-time error '13' (type mismatch).

    Any suggestions? Thanks
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    How about
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    Thanks Fluff, seemed to do the trick

    However, it seems that the code adds columns that already exist, so long as they appear in a different order than in the table.

    Is there a way around this? E.g. if "Column 4" appears in Column D, it is still added if "Column 4" is e.g. the first row in the list

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    How about
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    Perfect, great stuff.

    One last question (which is not relevant right now but might be soon), if it requires just a simple tweak.

    If I wanted the sheet to delete all columns that do not appear from the list, what would I have to do?

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    How about
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    Yes, thanks! Except it removes the very last column that it just added

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    Sounds like there weren't any extra columns to remove, try
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    That tweak simply adds back all (both deleted and kept columns) to the right of the last column.

    Last code worked perfectly, except it removed the very last column that it added/kept

  10. #10
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    Not for me it doesn't, do you have anything in row1 other than the headers?

  11. #11
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    Nope - error occurs once you run the code twice (works fine first time, but if you run it again without changing anything it bugs)

  12. #12
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    Nope, I can't replicate that behaviour, so not sure what's happening.

  13. #13
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: VBA to insert missing columns based on a list

    Hmm, weird. I will find a solution.

    Anyways, thanks a lot for all the help, greatly appreciated!

  14. #14
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: VBA to insert missing columns based on a list

    You're welcome & thanks for the feedback.

+ 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] Compare two columns (from different sheets) delete if no match and insert if missing
    By NametobeRenamed in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2020, 10:55 AM
  2. [SOLVED] Compare two columns on separate sheet, if missing, insert missing data
    By lilvictorians in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-03-2019, 11:44 PM
  3. insert missing dates in columns vba
    By matija385 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-07-2016, 11:15 AM
  4. Insert columns based on a variable sized list
    By NickiP in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 12-09-2014, 12:03 AM
  5. [SOLVED] insert missing date columns
    By Hang Glider in forum Excel General
    Replies: 4
    Last Post: 10-21-2014, 03:32 PM
  6. Excel Macro to find missing dates and insert missing date as blank row
    By JevaMarie in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-26-2013, 11:08 AM
  7. insert missing columns
    By chi11 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-16-2012, 06:32 PM

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