• Batch file to copy sheet

    Author
    Topic
    #484625

    I would like to have a batch file that would copy a sheet from a spreadsheet to each of 185 spreadsheets.
    The sheet to be copied would be [count.xlsx]Count!
    The spreadsheets are ARM1.xlsx through ARM185.xlsx.

    suggestions?
    thanks!

    Viewing 2 reply threads
    Author
    Replies
    • #1343336

      J.E.

      This VBA placed in the count.xlsx workbook and renamed to count.xlsm should do the trick.

      Code:
      Option Explicit
      
      Sub MassCopySheet()
      
         Dim wkbkSource     As Workbook
         Dim wsSource       As Worksheet
         Dim wkbkDest       As Workbook
         Dim iWkBKCntr      As Integer
      
         Application.ScreenUpdating = False
      
         Set wkbkSource = ActiveWorkbook
         
         Set wsSource = wkbkSource.Sheets("Count")  'Source Sheet to copy
      
         For iWkBKCntr = 1 To 185
      
            On Error Resume Next
            Set wkbkDest = Workbooks.Open("ARM" & Format(iWkBKCntr) & ".xlsx")
            On Error GoTo 0
            
            If Not wkbkDest Is Nothing Then
              wsSource.Copy After:=wkbkDest.Sheets(1)
              Application.DisplayAlerts = False
              With wkbkDest
                  .Save
                  .Close
              End With
            End If  'wkbkDest  Nothing
            
         Next iWkBKCntr
         
      End Sub
      

      Notes:
      1. The code compiles correctly.
      2. The code runs correctly to end. Of course I don’t have any of the ARM workbooks so all I’ve really tested is the error trap for missing files in the series, which works.
      3. You need to add a drivepath to the destination workbook name if it is not in the same directory as the count.xlsx file.
      4. If you don’t want the copied sheet to follow Sheet 1 then you need to change the AFTER clause of the Copy command with the appropriate number or with the Sheet Name in quotes.
      :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

      • #1343782

        thanks for saving me a lot of drudgery!
        greatly appreciated

      • #1343842

        Hi AllPardon me but why was a solution made of linking the worksheet cells in all these workbooks passed?I thought of this as I was reading the post.Any limitations on linking?Wassim

    • #1343366

      An ordinary BATch file only knows about files, and could not access a workbook within a spreadsheet.
      So RetiredGeek’s VBA method is the way to go.

      BATcher

      Plethora means a lot to me.

    • #1343862

      Wassim,

      Somehow, I don’t think linking 185 workbooks is a very good idea. :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 2 reply threads
    Reply To: Batch file to copy sheet

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: