0

I'd like to copy a sheet with formulas to other workbooks, so it is important to have a general target, which I can use for other workbooks, as well, not just for one. Here is my code:

Sub Macro1()

    Windows("Filefromcopy.xls").Activate
    Sheets("needtocopy").Select
    Sheets("needtocopy").Copy Before:=Workbooks("target.xls").Sheets(1)
End Sub

Could you please give me some help?

Thanks!

Smittey
  • 2,475
  • 10
  • 28
  • 35
Sz_Csill
  • 1
  • 2

1 Answers1

0

I'd do it like this:

Sub Button1_Click()
    Dim source_worksheet As Worksheet
    Dim source_workbook As Workbook
    Dim target_workbook As Workbook

    Set source_workbook = ActiveWorkbook
    Set target_workbook = Workbooks.Add()
    Set source_worksheet = source_workbook.Sheets("needtocopy")

    source_worksheet.Copy Before:=target_workbook.Sheets(1)
End Sub

This piece of code is a bit verbose but the purpose is to give you an idea where things are happening.

The selection of the target workbook is done on the Set target_workbook row. Here you can create a new workbook (like I'm doing it in this example), or open an existing workbook.

To open an exisiting workbook, replace the Set target_workbook row with this:

Set target_workbook = Workbooks.Open("target.xls")

At the end, you can add some saving and closing functionality as well:

target_workbook.Save
target_workbook.Close

To get a list of files in a folder you need to define an object with Dir (as described in this post: Loop through files in a folder using VBA?):

Sub Button1_Click()
Dim source_worksheet As Worksheet
Dim source_workbook As Workbook
Dim target_workbook As Workbook

Set source_workbook = ActiveWorkbook
Set source_worksheet = source_workbook.Sheets("needtocopy")

Dim file As Variant
Dim folder As String
folder = "C:\test\"
file = Dir(folder)
Application.DisplayAlerts = False
While (file <> "")
    If InStr(file, "Allocation") <> 0 Then
        Set target_workbook = Workbooks.Open(folder & file)
        source_worksheet.Copy Before:=target_workbook.Sheets(1)
        target_workbook.Save
        target_workbook.Close
    End If
    file = Dir
Wend
Application.DisplayAlerts = True
End Sub
Community
  • 1
  • 1
Gabor
  • 685
  • 2
  • 11
  • 25
  • Oh, it lookes nice, thanks! But actually my real problem is to have a general target, so I can use it every file which has the word "Allocation" in its file name. Could you please give me some ideas for that, as well? – Sz_Csill Aug 17 '16 at 08:38
  • I tried it and it didn't work for me. I haven't got any messages or debugs. When I open the target file, I only got a message if I enable or disable macros, and it doesn't copy the sheet.. I saved both the target and source file to the desktop and I give that location for the folder, but nothing happens.. – Sz_Csill Aug 17 '16 at 10:08
  • I've tested it again and it works for me. Did you change the path setting in the folder variable? Also, try to comment out the Application.DisplayAlerts = False to see any error messages. – Gabor Aug 17 '16 at 12:30