Results 1 to 20 of 20

Thread: Copy data only from multiple workbooks to Workbook with mulitple sheets using Macros

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Copy data only from multiple workbooks to Workbook with mulitple sheets using Macros

    Hi All,

    I am new to this forum so apologies if I am asking simple questions!

    I have an urgent problem which I have been trying to code for weeks but unfortunately my VB skills at basic to put it kindly.

    Excel version: Excel 2003

    I have one workbook c:\Documents and settings\khilan.dhanani\Summary Weekly Report\Weekly Report.xls (master) which contains the following sheets:

    Summary
    Minor Works Projects
    Optel T and T
    Remote Asset
    Control Telephony
    Livelink service
    .............etc there are 25 sheets in total

    I then have separate workbooks which are distributed for update containing 1 worksheet for each project:

    c:\Documents and settings\khilan.dhanani\Weekly Reports\Minor Works Projects.xls
    c:\Documents and settings\khilan.dhanani\Weekly Reports\Optel T and T.xls
    c:\Documents and settings\khilan.dhanani\Weekly Reports\Remote Asset.xls
    c:\Documents and settings\khilan.dhanani\Weekly Reports\Control Telephony.xls
    ..............etc there are 24 workbooks in total

    The main summary workbook (master) is password protected as are the sheets.

    What I need is a macro which switches the protection of then loops through each workbook and copies the values only from each work book in the range of B2:N34 where the project names on the worksheets match. The reason why I only want the values copied is because the formatting is all held in the master spreadsheet and also each project feeds into the summary sheet and copying the who sheet new seems to break the link and formulas

    So in simple terms in need the following

    Where Workbook (master). Remote Asset = Workbook 3.Remote Asset then copy/paste (values only) range B2:N34

    Hope that makes sense. I can upload a copy of the workbooks if needed.

    Will really, really, really appreciate any help with this

    Thank you in advance,

    Khilan

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Where exactly are you planning on running this from, the "master"? Are any of these spreadsheets going to be open? What is password protected, just the "Master", is it protected from opening, or just protection on the sheets, or both? Are they all the same passwords? Are any of the other sheets password protected?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Hi nO_OnE and thank you for the prompt reply.

    I would like to run the macro from the "master" spreadsheets which will be the only spreadsheet open. All the other spreadsheet will be stored in one network folder.

    The master workbook is protected to only allow users to update certain fields so each sheet is protected as are the sheets all other workbooks. All the wokbooks and sheets are protected using the same password.

    Hopefully that makes sense. Let me know if you need any further information

  4. #4
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Try this code:
    You need to change the "Password" to the your the actual password (Make sure you leave the "Quotations" on your password).
    Check the directories
    This also assumes that you do not want the 'Summary' sheet copied (you didn't have it listed in your first post)

    I haven't actually tested this code, so let me know if there is a problem, and where it is...
    vb Code:
    1. Public Sub CopyMaster()
    2.     Dim Pass As String
    3.         Pass = "Password"
    4.     Dim DirectoryPath As String
    5.         DirectoryPath = "c:\Documents and settings\khilan.dhanani\Summary Weekly Report\"
    6.    
    7.     Dim wb As Workbook
    8.         Set wb = Workbooks("Weekly Report.xls")
    9.    
    10.     Dim sht As Worksheet
    11.     Dim wbtmp As Workbook
    12.     Dim shttmp As String
    13.        
    14.     For Each sht In wb
    15.         'This assumes you do not want to copy the "summary" sheet, and skips it
    16.         If sht.Name = Summary Then
    17.             Next sht
    18.         End If
    19.        
    20.         Dim wbnm As String
    21.             wbnm = sht.Name & ".xls"
    22.        
    23.         'This will unprotect the sheet
    24.         sht.Unprotect Pass
    25.        
    26.         'This opens the workbook that has the same name as The sheet name
    27.         Workbooks.Open (DirectoryPath & wbnm)
    28.         Set wbtemp = (wbnm)
    29.    
    30.         'This copys the current sheet values into memory
    31.         sht.Range("B2:N34").Copy
    32.  
    33.         'This pastes the copied range's values into "Sheet1" of the target workbook
    34.         'starting at cell "B2"
    35.         wbtemp.Worksheets("Sheet1").Range("B2").PasteSpecial xlPasteValues
    36.        
    37.         'This saves and closes the target workbook
    38.         wbtemp.Save
    39.         wbtemp.Close
    40.        
    41.         'This will re-protect the sheet with the same password
    42.         sht.Protect Pass
    43.        
    44.     'moves to the next sheet
    45.     Next sht
    46.  
    47.  
    48. End Sub
    Last edited by nO_OnE; Nov 18th, 2011 at 08:17 PM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Hi nO_OnE

    Sorry about the delayed reply and thank you so much for taking the time to help me with this, your efforts are really appreciated.

    I have added the code but seem to get a compile error : Next without For
    on line no.17

    Any ideas?

  6. #6
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Try this instead...

    vb Code:
    1. Public Sub CopyMaster()
    2.     Dim Pass As String
    3.         Pass = "Password"
    4.     Dim DirectoryPath As String
    5.         DirectoryPath = "c:\Documents and settings\khilan.dhanani\Summary Weekly Report\"
    6.    
    7.     Dim wb As Workbook
    8.         Set wb = Workbooks("Weekly Report.xls")
    9.    
    10.     Dim sht As Worksheet
    11.     Dim wbtmp As Workbook
    12.     Dim shttmp As String
    13.  
    14.     For Each sht In wb
    15.         'This assumes you do not want to copy the "summary" sheet, and skips it
    16.         If sht.Name <> "Summary" Then
    17.        
    18.             Dim wbnm As String
    19.                 wbnm = sht.Name & ".xls"
    20.        
    21.             'This will unprotect the sheet
    22.             sht.Unprotect Pass
    23.        
    24.             'This opens the workbook that has the same name as The sheet name
    25.             Workbooks.Open (DirectoryPath & wbnm)
    26.             Set wbtemp = (wbnm)
    27.    
    28.             'This copys the current sheet values into memory
    29.             sht.Range("B2:N34").Copy
    30.  
    31.             'This pastes the copied range's values into "Sheet1" of the target workbook
    32.             'starting at cell "B2"
    33.             wbtemp.Worksheets("Sheet1").Range("B2").PasteSpecial xlPasteValues
    34.        
    35.             'This saves and closes the target workbook
    36.             wbtemp.Save
    37.             wbtemp.Close
    38.        
    39.             'This will re-protect the sheet with the same password
    40.             sht.Protect Pass
    41.        
    42.         End If
    43.     'moves to the next sheet
    44.     Next sht
    45. End Sub

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Hi nO_OnE.

    Thanks for the updated code

    I have added the code but seem to get a compile error : Object required
    on line no.26 (wbnm is highlighted)

    Any ideas?

  8. #8
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Change that line to:

    Code:
           Set wbtemp = Woorkbooks(wbnm)

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Runtime Error 438 - Object doesn't support this property or method

    Line 14 . For each sht in wb is highlighted when debugging

  10. #10
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Just a thoght. Why dont you use direct references or queries to the original workbooks instead of trying to copy the values?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Unfortunately due to the distribution of the master spreadsheet once populated it is not possible to query or reference the data.

    I agree though it would have been alot easier to do so.

  12. #12
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Ok, I see the problem there...

    Line 14 should be:
    Code:
    For Each sht In wb.sheets
    I seen another problem, which is the source file, and which is the target? are you copying the information from the "Master" to the individual workbooks, or are you copying the data from the other workbooks to the "Master"?

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Thanks i'll try that.

    I am copying the data from the separate workbooks to the master which can be stored in two separate locations/folders or the same folder whichever is easier

  14. #14
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Change the "Password" to the actual password (remember to keep the "")

    This also assumes that the data is on "Sheet1" of the source workbooks.

    vb Code:
    1. Public Sub CopyMaster()
    2.     Dim Pass As String
    3.         Pass = "Password"
    4.     Dim DirectoryPath As String
    5.         DirectoryPath = "c:\Documents and settings\khilan.dhanani\Summary Weekly Report\"
    6.    
    7.     Dim wb As Workbook
    8.         Set wb = Workbooks("Weekly Report.xls")
    9.    
    10.     Dim sht As Worksheet
    11.     Dim wbtmp As Workbook
    12.     Dim shttmp As String
    13.  
    14.     For Each sht In wb.Sheets
    15.         If sht.Name <> "Summary" Then
    16.             Dim wbnm As String
    17.                 wbnm = sht.Name & ".xls"
    18.             sht.Unprotect Pass
    19.             Workbooks.Open (DirectoryPath & wbnm)
    20.             Set wbtemp = Workbooks(wbnm)
    21.             wbtemp.Worksheets("Sheet1").Range("B2:N34").Copy
    22.             sht.Range("B2").PasteSpecial xlPasteValues
    23.             wbtemp.Save
    24.             wbtemp.Close
    25.             sht.Protect Pass
    26.         End If
    27.     Next sht
    28. End Sub
    Last edited by nO_OnE; Nov 21st, 2011 at 07:46 PM.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Hiya

    The code works up until is skipped the summary sheet and looks for the next sheet but is for some reason looking for Sheet2.xls in the directory. I renamed one of the xls to Sheet1.xls and renamed the worksheet to Sheet1 but this also failed

    The master spreadsheet is made up the following sheet in the following order
    Summary, Optel TandT, Remote Asset, Control Telephony etc....

    And the spreadsheet that feed the data are named as follows sheet name in brackets
    Optel TandT.xls (Optel TandT)
    Remote Asset.xls (Remote Asset)
    Contol Telephony.xls (Control Telephony)

    I can rename the sheets on the individual spreadsheet to sheet 1 if that is easier but the master has to remain as is.

    So Optel TandT.xls (Optel TandT) need sto write to the Optel TandT worksheet within the master spreadsheet, Remote Asset.xls (Remote Asset) needs to write to the Remote Asset worksheet within the master spreadsheet and so on


    Hope that helps?

  16. #16
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    So the sheet name on the source workbooks are going to be the same names as on the "Master"?

    I am not sure what you are asking about the summary, is there a summary workbook?

    I modified the last post so it would have line numbers,
    To fix the sheet name issue, replace Line 21 with:
    Code:
                wbtemp.Worksheets(Sht.name).Range("B2:N34").Copy

  17. #17
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Quote Originally Posted by nkkhil View Post
    Unfortunately due to the distribution of the master spreadsheet once populated it is not possible to query or reference the data.

    I agree though it would have been alot easier to do so.
    Excuse me for insisting but you could distribute a version of the master spreadsheet that has no external connections and still do it with queries. One option is to have the query built in the macro, execute it and copy the result. This would be much more efficient than oppening each workbook to copy and paste, which in itself is very resource consuming.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Posts
    17

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Hi nO_OnE, That code it working now. Thanks you so much for your help I really appreciate it. It has also spurred me to get more educated in the use of macros and general excel functionality. Thanks once again

    Hi kaliman79912,

    Unfortunately i am what you might call an excel amatuer but alway open to learn new functions. How is it possible to have a version of the master spreadsheet that has no external connections and still do it with queries?

  19. #19
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    Glad its working...
    VBA opens a lot of doors, doors that most people didn't realize even existed

  20. #20
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Copy data only from multiple workbooks to Workbook with mulitple sheets using Mac

    You can create the query in code and destroy it after it retrieves the data.

    Here's an example on how to create it:

    vb.net Code:
    1. Sub CreateQT()
    2.  
    3.     Dim sConn As String
    4.     Dim sSql As String
    5.     Dim oQt As QueryTable
    6.  
    7.     sConn = "DSN=Excel Files;DBQ=C:\Reps\OT.xlsm;" & _
    8.         "DefaultDir=C:\Reps\;DriverId=1046;" & _
    9.         "MaxBufferSize=2048;PageTimeout=5;"
    10.  
    11.     sSql = "SELECT `CUTLOT$`.CUORD, " & _
    12.         "FROM `CUTLOT$` GROUP BY `CUTLOT$`.CUORD"
    13.  
    14.    Set oQt = ActiveSheet.QueryTables.Add( _
    15.         Connection:=sConn, _
    16.         Destination:=Range("a1"), _
    17.         Sql:=sSql)
    18.  
    19.     oQt.Refresh
    20.  
    21. End Sub

    To delete the query:

    vb.net Code:
    1. Sheets("Sheet1").QueryTables.Item("MyTable").Delete

    This disconects the query but does not delete the data
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

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