+ Reply to Thread
Results 1 to 3 of 3

Working with a combined workbook macro and need to add filename from each source book to C

Hybrid View

  1. #1
    Registered User
    Join Date
    09-11-2014
    Location
    Kansas City, MO
    MS-Off Ver
    2010
    Posts
    2

    Working with a combined workbook macro and need to add filename from each source book to C

    Excel Ver. 2010

    I have built a WB that combines the data from 50 or so WBs with a list of the files in Tab "List" and the data from each WB into Tab "Data"

    I need to import the file name and write it into Column A of the Data tab. I know I will need to move the copy to column 2 and can do that. My problem is that the only code I see is how to fill data that is already in the source sheet such in the code below. My file name is not in the data sheets I am combining, so the code below does not work for that.

    I am using a list of the files from the Tab "List"

    Code from Live Lessons: Excel VBA and Macros with Mr Excel:

     ' Replicate the department from A2 to column A in WSD
     WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value = WSN.Range("A2")
     Sub CombineFiles()
     Dim WBO As Workbook ' original workbook
     Dim WBN As Workbook ' individual data workbooks
     Dim WSL As Worksheet ' List of files worksheet
     Dim WSD As Worksheet ' data collection worksheet
     Dim WSN As Worksheet
    
     ' Define object variables
     Set WBO = ThisWorkbook
     Set WSL = WBO.Worksheets("List")
     Set WSD = WBO.Worksheets("Data")
    
     Application.ScreenUpdating = False
    
     ' Clear out any previous data on WSD, but leave the headings
     WSD.Cells(2, 1).Resize(Rows.Count - 1, Columns.Count).Clear
    
     NextRow = 2
    
     ' Loop through all the files on WSL
     FinalRow = WSL.Cells(Rows.Count, 1).End(xlUp).Row
     For i = 1 To FinalRow
     ThisFile = WSL.Cells(i, 1)
     ' Open a file
     Set WBN = Workbooks.Open(Filename:=ThisFile)
    
     For Each WSN In WBN.Worksheets
    
     ' Last row in this file? RowCount is 4 less <- Not Used Here use -1 to not grab headers.
     LastRow = WSN.Cells(Rows.Count, 1).End(xlUp).Row
     RowCount = LastRow - 1
    
     ' Copy from 5 to last row over to column B
     WSN.Cells(2, 1).Resize(RowCount, 20).Copy Destination:=WSD.Cells(NextRow, 1)
    
     -> ' Replicate the department from A2 to column A in WSD
     -> ' WSD.Cells(NextRow, 1).Resize(RowCount, 1).Value = WSN.Range("A2")
    
     ' Set up the new NextRow
     NextRow = NextRow + RowCount
     Next WSN
    
     ' Close WSN, don't save
     WBN.Close SaveChanges:=False
    
     Next i
    
     MsgBox "FILES HAVE BEEN PROCESSED"
     Application.ScreenUpdating = True
     End Sub
    Thanks!
    -SS
    Last edited by ShadowSaxx; 09-16-2014 at 12:05 PM. Reason: code tags

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,938

    Re: Working with a combined workbook macro and need to add filename from each source book

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-11-2014
    Location
    Kansas City, MO
    MS-Off Ver
    2010
    Posts
    2

    Re: Working with a combined workbook macro and need to add filename from each source book

    Yes, the code works.

    I have been able to bring all of the data from the WBs into the data sheet. I just need to know what file it came from.

    -SS

+ 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: 2
    Last Post: 09-14-2014, 04:38 PM
  2. Save as dialog box, value from listbox in filename combined with text
    By Masun in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-15-2013, 08:53 AM
  3. Want to Hide a UserForm source book while keeping main XL workbook open...
    By MBlaster in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-03-2012, 05:52 PM
  4. Replies: 0
    Last Post: 11-01-2012, 09:28 AM
  5. macro not working once filename changes
    By XCESIV in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-02-2006, 10:45 PM

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