+ Reply to Thread
Results 1 to 18 of 18

Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

  1. #1
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Question Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Hi all.

    Ok i'm pretty new to Macros so i'm pretty newbish so don't even know where to start.

    I'm working in a company and we are doing call trackers where people fill in data into a couple of columns in rows downwards.

    Starts at Row 3 and all data between column B to H.

    What i have now is a master workbook which has multiple sheets for management.
    Each analyst has a workbook with just one sheet.

    I want sheet1 of all analysts to be copied to sheet1 in my master workbook.

    All workbooks are protected so need to add code for that.

    All workbooks are in seperate folders but they are all subfolders to one parent folder..
    which is \\ph-fs002\project\jes\

    Thing is that there are multiple workbooks in each folder but i just want it to use one of them. possibly can i have a box above where i write the name it will look for(they will all be called the same)

    So basicly it needs to go into each, unprotect it, copy all data between B3:H500, send into next blank field of my workbook and loop this untill it's done with all and also want it to show in the end a message saying # logsheets merged successfully if it didnt work then just failed or something and if possible showing why it failed.

    know if you do this it will prompt to save each one because each is opened.. some way to go around this? each will not need to be saved as then they wont be protected anymore.. can press cancel over and over again but would be easier if there was a way around it.

    I want this activated by a button btw if that matters..

    If needed my workbook can be in one of the subfolders as well..

    Anymore information needed?

    Hopefully someone can help, been working on this for days now without getting anywhere and the dead line for completion of this i nearing

    Best regards
    Christian

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Are you looking to hire a consultant, or are you coding and have some specific questions?

    For self-starting, here's a macro for collecting data from all files in a specific folder.
    The parts of the code that need to be edited are colored to draw your attention. that will get you many steps closer to your goal.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Hey. thanks for the reply.

    The code i'm playing around with is below.

    Can't figure out where it fails. It does merge something but for some reason its some timestamps going into sheet 6.. Want everything from sheet 1.. to go into sheet 1 in my main to the next blank row. Any idea whats wrong?

    Would also need to add a unprotect code as this is needed i would think or less it wont copy over or?

    Hope someone can help with this.

    It's going btw from B3 to G1000. Sheet1!B3:G1000 should be copied to the workbook(where the code is in) in sheet1 starting at first blank row pasting to B:G.. A should be left alone as thats just numbers and will be a total count in the end.

    Option Explicit

    Sub ImportSameFileNameDataAllSubFolders()


    Dim fNAME As String: fNAME = "test.xlsm"
    Dim fPATH As String: fPATH = "d:\logsheettesting\merge\"
    Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
    Dim SubFLDRS As Object: Set SubFLDRS = FLD.Subfolders
    Dim SubFLD As Object
    Dim wbMain As Workbook: Set wbMain = ThisWorkbook
    Dim wbData As Workbook
    Dim ws As Worksheet
    Dim LR As Long
    Dim wsTest As String

    Application.ScreenUpdating = False

    For Each SubFLD In SubFLDRS
    Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "\" & fNAME)

    On Error Resume Next

    For Each ws In ActiveWorkbook.Worksheets
    LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
    ws.Range("b3:F1000" & LR).EntireRow.Copy
    With wbMain 'add sheet if needed
    If Not Evaluate("ISREF('[" & wbMain.Name & "]" & ws.Name & "'!$A$1)") Then _
    .Worksheets.Add(After:=.Worksheets(.Worksheets.Count)).Name = ws.Name
    .Sheets(ws.Name).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
    End With
    Next ws

    Application.CutCopyMode = False
    wbData.Close False
    Next SubFLD

    Set wbMain = Nothing
    Application.ScreenUpdating = True
    End Sub

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Please Login or Register  to view this content.
    Looks like you left the macro to look in column A when you wanted it to look in column B.

  5. #5
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    This worked! however not 100%... I created 6 test folders with one file in each... same info in all.. jut file 1, 2 and 6 were merged.. deleted 6 to try and then it took 1, 2 and 5... any idea on why? also could someone help me add a message box in the end that lists how many files were merged?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Something in your environment? You can F8 through your macro to execute one line of code at a time and see what it's doing, hover the mouse over variables to see the current values, etc. Forensics time.

  7. #7
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Yea sadly it doesnt really show anything at all as it does show and wont give any error. It just doesnt take all folders for some reason. Also i will need to add a code to make it just check in each folder and see if it has the listed file i want it to copy, if not found then continue to next file. Now it shows a error if the file isnt in each folder.. The folder has **** loads of other folders which does not include the xlsm files at all so need to add this or it will be useless sadly.. any help?

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Since I use that macro, no, I don't know why your environment is skipping folders.

    As for testing whether a file exists or not:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Hey sorry for late response.

    Yes it does work but the problem is that it gives a error for each folder the file is not in and for each one it does not find the expected file it actually copies the mainfile to itself actually...

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    The errors messages I don't understand since we use on the On Error Resume Next. I don't get that. But there are other ways to test if the file exists or not BEFORE trying to actually open it:

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    thanks but adding that i get compile error: next without for.

    The error i get when trying to merge before adding this when it does not find the file in a folder is "path\testing.xls" cannot be found. Check your spelling or try a different path.

    So it shows that for each folder the file is not in. And for each folder the file is not in, the main file copies and pastes to self which is rather annoying as there are 6 folders and i then get 6*6 copies...

    On error resume next is over for each subfld in subfldrs. Tried having it under there as well.. same stuff.

    Any idea?

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    hmm... maybe:
    Please Login or Register  to view this content.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    So it shows that for each folder the file is not in. And for each folder the file is not in, the main file copies and pastes to self which is rather annoying as there are 6 folders and i then get 6*6 copies...
    I don't get that at all, I see no reason in the original macro that it should ever reference itself.

  14. #14
    Registered User
    Join Date
    03-02-2012
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    How can i change this code to just work for one sheet instead of the full workbook. I think that part might be causing some problems.
    I've been trying to make some changes with no luck.

    i'm using:
    Option Explicit

    Sub Em()


    Dim fNAME As String: fNAME = ActiveWorkbook.Sheets("Sheet1").Cells(4, 10).Value
    Dim fPATH As String: fPATH = "D:\Testing\Testing\"
    Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
    Dim FLD As Object: Set FLD = FSO.GetFolder(fPATH)
    Dim SubFLDRS As Object: Set SubFLDRS = FLD.Subfolders
    Dim SubFLD As Object
    Dim wbMain As Workbook: Set wbMain = ThisWorkbook
    Dim wbData As Workbook
    Dim ws As Worksheet
    Dim LR As Long
    Dim wsTest As String

    Application.EnableEvents = False
    Application.ScreenUpdating = False


    On Error Resume Next
    For Each SubFLD In SubFLDRS

    Set wbData = Workbooks.Open(fPATH & SubFLD.Name & "\" & fNAME)

    For Each ws In ActiveWorkbook.Worksheets
    LR = ws.Range("B" & ws.Rows.Count).End(xlUp).Row
    ws.Range("B3:H500" & LR).Copy
    With wbMain
    If Not Evaluate("ISREF('[" & wbMain.Name & "]" & ws.Name & "'!$A$1)") Then _
    .Worksheets.Add(After:=.Worksheets(.Worksheets.Count)).Name = ws.Name
    .Sheets(ws.Name).Range("E" & Rows.Count).End(xlUp).Offset(1).Offset(0, -3).PasteSpecial xlPasteValues
    End With

    Application.CutCopyMode = False
    wbData.Close False

    Next ws

    Next SubFLD

    Set wbMain = Nothing

    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Hm, I see nothing wrong. I'll ask some others to look at it.

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    Try this:
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    01-19-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    how to split the data with multiple sheet to multiple workbook based on coloum




    if sheet 1 has many data and also given the name chetan raj amit
    and sheet 2 has given in pivot table who is linked in sheet1
    and sheet 3 has given in chart who is linked in sheet1

    then what should i do in this case
    simple save as and delete the data

    i need 3 workbook
    1 chetan workbook with 3 sheets( 1 data, 2 pivot table, 3 chart)
    2 raj workbook with 3 sheets ( 1 data, 2 pivot table, 3 chart)
    3 amit workbook 3 sheets ( 1 data, 2 pivot table, 3 chart)


    so can u help me
    of this matter
    any vb code

  18. #18
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Merge mulitiple workbooks into sheet with code in it. Protected books. Urgent

    As per forum rules, please start a thread of your own. Attach a sample workbook to help contributors assess your question. Provide links to other threads you've read and find possibly relevant.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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