Results 1 to 19 of 19

Thread: **Resolved**Combining Multiple Excel Sheets

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    **Resolved**Combining Multiple Excel Sheets

    Ok here is the issue:

    I have a macro that runs a report. It changes one cell in the report(which updates it to a new cost center) and then saves a hardcoded copy to a folder and leaves the workbook open. It does this for about thirty reports.

    Now what I want is to do this....
    I have folders for thirty people named "John Doe", "Sally Mae", etc.

    After the macro has run the reports, and they are all open I want it to stick the appropriate file (which have names like 2431, 2541, 2786) in to the correct folder. Sometimes the file needs to go in to multiple people's folders.

    Now here is the kicker, sometimes a manager wants multiple reports and wants them all combined into one workbook.

    Any ideas?

    -Thanks!!!!!!!
    Last edited by gtg689a; Mar 16th, 2006 at 10:18 AM.

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    This should get you started. Note: you may want to disable screenupdating while this is running.
    You will need a range in you controling workbook that contains the manager name as trh column header and a list of each report for that manger under the header.
    VB Code:
    1. Sub BuildMgrReport()
    2. Const sPATH As String = "C:\data\gtg689a\" 'I used this path, but you will need to change it
    3.  
    4. Dim rngDistList As Range
    5. Dim lMgrNum As Long
    6. Dim wkbMgrReport As Workbook
    7. Dim lReportNum As Long
    8. Dim wkbSource As Workbook
    9.  
    10.     'Make sure we are in the correct directory
    11.     ChDir sPATH
    12.    
    13.     'A range that stores the Manager Name in the first row
    14.     'then the list of reports for that manager
    15.     Set rngDistList = ThisWorkbook.Worksheets("Distributions").Range("Distribution_List")
    16.    
    17.     'Repeat the following for each manager
    18.     For lMgrNum = 1 To rngDistList.Columns.Count
    19.        
    20.         'Creat a new workbook for the manager
    21.         Set wkbMgrReport = Application.Workbooks.Add
    22.        
    23.         'Save that book in the current directory
    24.         'Overwriting any existing file without a prompt
    25.         Application.DisplayAlerts = False
    26.         wkbMgrReport.SaveAs Filename:=rngDistList.Cells(1, lMgrNum).Value
    27.         Application.DisplayAlerts = True
    28.        
    29.         'Now loop through each report for that manager
    30.         For lReportNum = 2 To rngDistList.Rows.Count
    31.             'only continue if there is a report name
    32.             If rngDistList.Cells(lReportNum, lMgrNum).Value <> "" Then
    33.                
    34.                 'open the report
    35.                 Set wkbSource = Application.Workbooks.Open(rngDistList.Cells(lReportNum, lMgrNum).Value)
    36.                
    37.                 'copy the worksheet to the managers workbook
    38.                 wkbSource.Worksheets(1).Copy After:=wkbMgrReport.Worksheets(wkbMgrReport.Worksheets.Count)
    39.                
    40.                 'close the report without saving
    41.                 wkbSource.Close False
    42.             End If
    43.         Next lReportNum
    44.        
    45.         'close the mangers report, saving the changes
    46.         wkbMgrReport.Close True
    47.     Next lMgrNum
    48.    
    49.     'Clear the object variables
    50.     Set rngDistList = Nothing
    51.     Set wkbMgrReport = Nothing
    52.     Set wkbSource = Nothing
    53. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    Thanks man,

    In the controlling workbook(the one that initially runs the reports), there is a cell that links to the macro and tells it where to stick all of the files. Can I do something like that for this so that every month i can just change a cell instead of going back in to the macro?

    For example, this month, all the files got dumped into Feb F06, next month itll be March F06.

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    Sure
    Just change sPATH to a variable in the procedure and populate it based on the value in your cell.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    I am having trouble with setting the range of the distribution list. It crashes each time and highlights the line that sets the distribution list.

    I changed the worksheet name to the correct one, and the range is also correct. But it keeps messing up. any ideas/
    Last edited by gtg689a; Mar 13th, 2006 at 10:47 AM.

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    Can you post the code that you are using to set the range?
    Also post the error message your are receiving.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    Ok, I think the range is fine. I believe the problem is I'm not setting it to find the files correctly. When I run the macro, it tells me that it can not find the file "2517", which definitly exists in the directory that I want it to pull from. Here is the code:

    VB Code:
    1. Sub MISBuildMgrReport()
    2.  
    3.  
    4. Dim rngDistList As Range
    5. Dim lMgrNum As Long
    6. Dim wkbMgrReport As Workbook
    7. Dim lReportNum As Long
    8. Dim wkbSource As Workbook
    9. Dim sPATH As Variant
    10.  
    11.    
    12.     'Set the path- this is the directory that all of the files are located
    13.     Set sPATH = ThisWorkbook.Worksheets("MISDistrList").Range("B26")
    14.    
    15.     'Make sure we are in the correct directory
    16.     ChDir sPATH
    17.    
    18.     'A range that stores the Manager Name in the first row
    19.     'then the list of reports for that manager
    20.     Set rngDistList = ThisWorkbook.Worksheets("MISDistrList").Range("A1:O19")
    21.    
    22.     'Repeat the following for each manager
    23.     For lMgrNum = 1 To rngDistList.Columns.Count
    24.        
    25.         'Create a new workbook for the manager
    26.         Set wkbMgrReport = Application.Workbooks.Add
    27.        
    28.         'Save that book in the current directory
    29.         'Overwriting any existing file without a prompt
    30.         Application.DisplayAlerts = False
    31.         wkbMgrReport.SaveAs Filename:=rngDistList.Cells(1, lMgrNum).Value
    32.         Application.DisplayAlerts = True
    33.        
    34.         'Now loop through each report for that manager
    35.         For lReportNum = 2 To rngDistList.Rows.Count
    36.           'only continue if there is a report name
    37.             If rngDistList.Cells(lReportNum, lMgrNum).Value <> "" Then
    38.                
    39.               'open the report
    40.                 Set wkbSource = Application.Workbooks.Open(rngDistList.Cells(lReportNum, lMgrNum).Value)
    41.                
    42.              'copy the worksheet to the managers workbook
    43.                 wkbSource.Worksheets(1).Copy After:=wkbMgrReport.Worksheets(wkbMgrReport.Worksheets.Count)
    44.                
    45.              'close the report without saving
    46.                 wkbSource.Close False
    47.             End If
    48.         Next lReportNum
    49.        
    50.         'close the mangers report, saving the changes
    51.         wkbMgrReport.Close True
    52.     Next lMgrNum
    53.    
    54.     'Clear the object variables
    55.     Set rngDistList = Nothing
    56.     Set wkbMgrReport = Nothing
    57.     Set wkbSource = Nothing
    58. End Sub
    Last edited by gtg689a; Mar 15th, 2006 at 02:06 PM.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    What line are you getting the error on?

    Also, please mark your code with the [Highlight=VB] tags - it makes it much easier to read...
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    VB Code:
    1. 'open the report
    2.                 Set wkbSource = Application.Workbooks.Open(rngDistList.Cells(lReportNum, lMgrNum).Value)
    Last edited by gtg689a; Mar 15th, 2006 at 02:05 PM.

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    OK, I need you to re-run the code and debug when it throws the error. At that point you will need to determine what the values of lReportNum and lMgrNum are. (Hover your cursor over the variable in the code and it will pop-up)
    Then, find the value in the coresponding cell in the rngDistList range and post that value here.

    Also, to tag your code you need to put vbcode (in the square brackets) before it and /vbcode (in the square brackets) after it. If you click on the go Advanced button you can also select a section of your reply and then click on the VBCode button above to wrap your code in the tags.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    Ok the values are:
    1 report num= 2
    1 Mgr Num = 1
    The value in the rng distr list is "2517"

    Thanks for your help, and I think I got the [Highlight=VB] thing working.

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    I tried that filename and it works, so the problem must be with the directory structure.

    Are all the individual reports in the same directory?

    What is the fully qualified path to this directory?

    What is the value in "Worksheets("MISDistrList").Range("B26")"? I.e. what is the value of the sPATH variable?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    Yes, they are all in the directory below.

    The full path is:
    G:\BPFA\F06\Closing Process\Monthly Reporting\Cost Center Reporting\February F06\MIS

    The value in B26 is:
    G:\BPFA\F06\Closing Process\Monthly Reporting\Cost Center Reporting\February F06\MIS

  14. #14
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    This might be a long shot but, try replacing
    VB Code:
    1. Set wkbSource = Application.Workbooks.Open(rngDistList.Cells(lReportNum, lMgrNum).[B]Value[/B])
    with
    VB Code:
    1. Set wkbSource = Application.Workbooks.Open(rngDistList.Cells(lReportNum, lMgrNum).[B]Text[/B])
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    I'm just going to post the actual file, if you don't have any ideas, I appreciate all of the help you have given me.

    If you get a second to look at it, I am stumped to say the least.
    Attached Files Attached Files

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    I fixed it a lot of the problem. In order for it to find the correct file, I changed the reports listed under each manager name from simply "2517" to "G:\BPFA\Filepath.....2517.xls. So now the macro runs, and it grabs all of the correct files, the only problem is that it does not save them in the correct directory, it saves them to My Documents.

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    OK I'm an idiot.
    The problem is that you are trying to change the directory without first changing the drive to "G". ChDir only works within a drive. You will need to change to the G: drive before moving to the file path.

    replace
    VB Code:
    1. ChDir sPATH
    with
    VB Code:
    1. ChDrive "G"
    2. ChDir sPATH

    That, I hope, should do it.

    Also, with this change you will not need the full path in the report list, only the file name.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    74

    Re: Combining Multiple Excel Sheets

    Thank you so much!! That really is beautiful.
    I added this to the code to delete sheets 1-3 that are not overwritten when the new workbook is opened.

    VB Code:
    1. 'Delete sheets 1,2,and 3 on the new reports
    2.          Sheets("Sheet1").Select
    3.          ActiveWindow.SelectedSheets.Delete
    4.          Sheets("Sheet2").Select
    5.          ActiveWindow.SelectedSheets.Delete
    6.          Sheets("Sheet3").Select
    7.          ActiveWindow.SelectedSheets.Delete
    8.         'close the mangers report, saving the changes
    9.         wkbMgrReport.Close True


    It runs well, the only problem is this warning message pops up:
    Data may exist in the sheets selected for deletion. To permanantly delete the data, press delete

    Is there any way to prevent that?

  19. #19
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Combining Multiple Excel Sheets

    Yes you can disable alerts before you delete the sheet and re-enable it when you are done.

    Rather than selecting each sheet before you delete it, you can just loop through the sheets to be deleted. The Excel application object has a property called SheetsInNewWorkbook that is useful here.

    You should insert the following into the procedure just before you close "wkbMgrReport". You will need to declare lSheetNum as a 'Long' variable type.
    VB Code:
    1. 'turn off user messaging
    2.     Application.DisplayAlerts = False
    3.    
    4.     'Remove the default number of sheet
    5.     For lSheetNum = Application.SheetsInNewWorkbook To 1 Step -1
    6.         wkbMgrReport.Worksheets(lSheetNum).Delete
    7.     Next lSheetNum
    8.    
    9.     'turn user messaging back on
    10.     Application.DisplayAlerts = True
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width