+ Reply to Thread
Results 1 to 6 of 6

Open workbooks and paste in value

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2015
    Location
    Newcastle, England
    MS-Off Ver
    2010
    Posts
    37

    Open workbooks and paste in value

    Hi All,

    I'm currently trying to write some code that does the following: (a) opens and unprotects workbooks (the names of which are listed), (b) copies and pastes a value into a specific cell on each sheet of the open workbooks, (c) saves, protects and closes the workbooks that have been opened.
    I'm not sure how best to go about this, and have an example to try and illustrate what I'm trying to achieve. I would like to write code that passes the workbook names through and carries out the tasks above. Would it be a good idea to put the workbook names in an array?

    Any advice would be greatly appreciated.TestData(111115).xlsm

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,890

    Re: Open workbooks and paste in value

    What is the path of the folder that contains the workbooks listed in D2:D4?
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    08-02-2015
    Location
    Newcastle, England
    MS-Off Ver
    2010
    Posts
    37

    Re: Open workbooks and paste in value

    Thanks for the reply Mumps1
    Currently they're in My Documents whilst I'm building the code, but eventually they'll be on a sharepoint site and therefore will need to be checked in and out (I don't yet have the sharepoint file path); the current file path is as follows: H:\Documents\Accounts\ReportingData
    Hope that helps...

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,890

    Re: Open workbooks and paste in value

    This macro will loop through all the files in your folder ("H:\Documents\Accounts\ReportingData\") so make sure that the folder contains only the workbooks to which you want to copy the data. Eventually, you will have to change the folder path.
    Sub LoopThroughFolders()
        Application.ScreenUpdating = False
        Dim ws As Worksheet
        Dim wbSource As Workbook
        Set wbSource = ThisWorkbook
        Dim wbOpen As Workbook
        Const strPath As String = "H:\Documents\Accounts\ReportingData\"
        ChDir strPath
        strExtension = Dir("*.xls*")
        Do While strExtension <> ""
            Set wbOpen = Workbooks.Open(strPath & strExtension)
            If wbOpen.Name = "Workbook A.xlsx" Then
                With wbOpen
                    .Unprotect
                    For Each ws In .Sheets
                        ws.Range("Q2") = wbSource.Sheets("Control Sheet").Range("B1")
                    Next ws
                    .Protect
                    .Close True
                End With
                strExtension = Dir
            Else
                With wbOpen
                    .Unprotect
                    For Each ws In .Sheets
                        If ws.Name <> "Summary" Then
                            ws.Range("Q2") = wbSource.Sheets("Control Sheet").Range("B1")
                        End If
                    Next ws
                    .Protect
                    .Close True
                End With
                strExtension = Dir
            End If
        Loop
        Application.ScreenUpdating = True
    End Sub

  5. #5
    Registered User
    Join Date
    08-02-2015
    Location
    Newcastle, England
    MS-Off Ver
    2010
    Posts
    37

    Re: Open workbooks and paste in value

    Many thanks for this Mumps1 - this is super helpful; I'm currently trying to split my code into different sections, I am going to try and have a sub function which checks out, opens and unprotects each of the listed workbooks, and a sub routine that saves, protects, closes and checks in the listed workbooks so that they can be called elsewhere in the code when needed.
    The check out, open and unprotect code I've attempted is below, does this look correct to you? I've defined the file paths and file names in the main body of the code and passed them to the sub routine:

    Sub CheckOutOpen(strFullDesc1 As String, strFullDesc2 As String, strFullDesc3 As String)
    If Application.Workbooks.CanCheckOut(strFullDesc1, strFullDesc2, strFullDesc3) = True Then
        Application.Workbooks.CheckOut strFullDesc1
        Application.Workbooks.Open strFullDesc1
        '.Unprotect workbook 1 here
        Application.Workbooks.CheckOut strFullDesc2
        Application.Workbooks.Open strFullDesc2
        '.Unprotect workbook 2 here
        Application.Workbooks.CheckOut strFullDesc3
        Application.Workbooks.Open strFullDesc3
        '.Unprotect workbook 3 here
    End If
    Thanks again...

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,890

    Re: Open workbooks and paste in value

    My apologies, but I'm not familiar with the 'Checkout' function.

+ 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. Open all workbooks in a folder and copy the first seeht> Paste to New Main workbook.
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-22-2014, 01:56 PM
  2. [SOLVED] Open Multiple Workbooks, Record names, Copy paste to Active Workbook, Close the Workbooks
    By vba_madness in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 06:09 AM
  3. Need help modifying a macro to open up workbooks and paste filtered data
    By LinkiePoo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2012, 04:14 PM
  4. Open all workbooks - copy / paste certain cells
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-14-2011, 06:28 AM
  5. Copy/Paste, between open workbooks,syntax
    By JapanDave in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-01-2011, 10:15 AM
  6. Copy and Paste Data between Two Open Workbooks
    By 3link in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2011, 12:07 PM
  7. Copy & paste between open workbooks
    By Robotacha2010 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-16-2010, 11:13 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