+ Reply to Thread
Results 1 to 15 of 15

Insert multiple columns in multiple workbooks

  1. #1
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Insert multiple columns in multiple workbooks

    I have about a hundred workbooks that I need to modify and I'm hoping to do that without having to open each one up individually. Each workbook only has data on one sheet.

    Briefly, I need to insert four columns in between 'H' and 'I'. These columns will have fixed titles across all the workbooks.

    Is it possible to do this by macro rather than opening up each workbook individually and inserting then re-naming each column? Looking for any guidance...

    Thanks!

  2. #2
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Insert multiple columns in multiple workbooks

    You can certainly have VBA do that for you. First 'teach' excel what you want done by recording a macro while you do it.
    he wrap code around the recorded VBA to do it for the other 100 worksbooks. You can use a list of file names with paths (especially if they are in different folders), or use a loop containg DIR to find them.

    I posted code here which shows how to parse folders for the files. Working from a list is much simpler.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    This will loop through all excel files in a folder, insert four columns, and add headers.
    Change the folder and headers to suit.
    First test this on a folder with a few test files.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 10-24-2013 at 06:07 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    Hi AlphaFrog, thanks for replying. I tried the code you posted, after changing the folder and headers, but the message box that popped up says '0 files processed'. Am I missing something?

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    Quote Originally Posted by sunrize9 View Post
    Hi AlphaFrog, thanks for replying. I tried the code you posted, after changing the folder and headers, but the message box that popped up says '0 files processed'. Am I missing something?
    Apparently. Show your modified code.

  6. #6
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    Sub Insert_Columns_All_Files()
    Dim strPath As String, strFile As String, Counter As Long

    strPath = "S:\VOI\FCC Cases\TEST"

    strFile = Dir$(strPath & "*.xls*", vbNormal)

    Application.ScreenUpdating = False
    Do While Len(strFile)
    Counter = Counter + 1
    With Workbooks.Open(strPath & strFile)
    With Sheets(1)
    .Columns("I:L").Insert
    .Range("I1:L1").Value = Array("Control Number", "Class", "Status", "Remarks")
    End With
    .Close SaveChanges:=True
    End With
    strFile = Dir$
    Loop
    Application.ScreenUpdating = True

    MsgBox Counter & " files proceesed. ", , "Insert Columns Complete"

    End Sub

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    Add a backslash to the end of the path

    strPath = "S:\VOI\FCC Cases\TEST\"

  8. #8
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    Thank you, it is now working albeit with one technical error: I saved five worksheets in the test folder and after the macro is run, the 4 columns are inserted once on the first sheet, twice on the second, three times on the third, etc.

    I'm trying to figure out what value to change so it only inserts those four columns and headings once on every workbook, but I'd appreciate any input.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    Quote Originally Posted by sunrize9 View Post
    Thank you, it is now working albeit with one technical error: I saved five worksheets in the test folder and after the macro is run, the 4 columns are inserted once on the first sheet, twice on the second, three times on the third, etc.

    I'm trying to figure out what value to change so it only inserts those four columns and headings once on every workbook, but I'd appreciate any input.
    I don't see how that could be. It works fine for me.

    Did you maybe have headers inserted already in some files before testing?

    The files in the folder don't have this macro code in them do they?

  10. #10
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    No they don't have headers or a code in them already. I moved five new test workbooks in the folder, but it's the same issue.

  11. #11
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    Quote Originally Posted by sunrize9 View Post
    No they don't have headers or a code in them already. I moved five new test workbooks in the folder, but it's the same issue.
    Well, I'm baffled.

    How are you calling or running this macro?

    Did you make any other changes to the code other than the folder and headers?

  12. #12
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    Do the five files have any other macro code in them?

  13. #13
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    So I open the second workbook in the test folder, and on the Visual Basic for Applications page and This Workbook button, I paste the macro you provided with only the folders and headers changed. After the macro runs, I get a pop-up:

    pop-up.PNG

    When I click 'No', I get a Runtime Error 1004:

    error.PNG

    When I click OK, the spreadsheet looks like this:

    headers.PNG

  14. #14
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Insert multiple columns in multiple workbooks

    1.) Put the macro code in a workbook that is not in the folder that is being called by the macro. In your case, the workbook file with this macro code is not in folder S:\VOI\FCC Cases\TEST\

    2.) Put the code in a standard code module e.g., Module1 not the ThisWorkbook code module.
    Alt+F11 to open the VBA editor
    From the VBA menu, select Insert\Module
    Paste the code in the new module
    If the code was installed in the ThisWorkbook code module, delete it from that location.

  15. #15
    Registered User
    Join Date
    10-24-2013
    Location
    Washington, DC
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Insert multiple columns in multiple workbooks

    I just got back to work on this project. Your suggestions worked perfectly and I'm able to insert those columns across all workbooks now without further difficulty. Thanks again!

+ 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. Replies: 20
    Last Post: 03-13-2013, 04:15 PM
  2. Insert page into multiple workbooks
    By OpieWinston in forum Excel General
    Replies: 5
    Last Post: 08-31-2011, 08:54 PM
  3. Insert a particular macro in multiple workbooks at once
    By dollar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-17-2009, 06:07 AM
  4. lookup a value from multiple columns of two workbooks
    By Asad - Shareef in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-09-2006, 06:15 PM
  5. [SOLVED] How do I insert multiple columns?
    By Lindsey M in forum Excel General
    Replies: 3
    Last Post: 03-16-2005, 09:06 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