1
votes

I recorded a macro to copy and paste from a master workbook to another (lets call it wbk1) however I cannot use this for all the other workbooks I need to format as the code in the macro specifically refers to wbk1.

How do I go about writing the code in such a way that it refers to the current open workbook and the master workbook.

I have tried to declare and refer the active workbook but it copies and pastes on the master

Windows("master.xlsm").Activate
Selection.Copy

Dim WbkCurrent As Workbook
Set WbkCurrent = ActiveWorkbook
WbkCurrent.Activate
Range("A1").Select
ActiveSheet.Paste

I expect the data to be pasted on whichever other workbook that is open along with the master workbook.

2

2 Answers

0
votes
Windows("master.xlsm").Activate
Selection.Copy

Dim WbkCurrent As Workbook
Set WbkCurrent = ActiveWorkbook
Dim wb As Workbook

For Each wb In Application.Workbooks
  If wb.Name <> WbkCurrent.Name Then
      'For the codeline below: assuming that you want to paste your selection to the first sheet for each wb. 
      'Else, change worksheets number to the sheet you want to copy to or change 
      'Worksheets(1)' into 'Activesheet' to copy on the currently active sheet for each wb.
      wb.Worksheets(1).Range("A1").PasteSpecial (xlPasteAll)

  End If
Next wb
0
votes

Copy from Master Book

This will copy the current selection of the Workbook named cMaster (master.xlsm) to the cell cRange (A1) of every other open workbook's ActiveSheet.

The Code

Option Explicit

Sub CopyFromMaster()

    Const cMaster As String = "master.xlsm"   ' Master Workbook
    Const cRange As String = "A1"             ' Paste Cell Range

    Dim wb As Workbook  ' Current Workbook

    ' Loop through all open workbooks.
    For Each wb In Workbooks
        ' Check if the name of the Current Workbook is different than the name
        ' of the Master Workbook.
        If wb.Name <> cMaster Then
            ' Copy the selection in Master Workbook to Paste Cell Range
            ' in ActiveSheet of Current Workbook.
            Windows(cMaster).Selection.Copy wb.ActiveSheet.Range(cRange)
        End If
    Next

End Sub

EDIT

Question in Comments

"How would I go about repeating the same instruction but now copying from sheet 2 of the master workbook and pasting in sheet 2 of the other open workbook?"

Sub CopyFromMaster2()

    Const cMaster As String = "master.xlsm"   ' Master Workbook
    Const cRange As String = "A1"             ' Paste Cell Range

    Dim wb As Workbook  ' Current Workbook

    For Each wb In Workbooks
        ' Check if the name of the Current Workbook is different than the name
        ' of the Master Workbook.
        If wb.Name <> cMaster Then
            ' Before using selection (select) you have to make sure that
            ' the worksheet is active or just activate it.
            Workbooks(cMaster).Worksheets("Sheet2").Activate
            ' Copy the selection in "Sheet2" of Master Workbook to
            ' Paste Cell Range "Sheet2" of Current Workbook.
            Selection.Copy wb.Worksheets("Sheet2").Range(cRange)
        End If
    Next

End Sub