+ Reply to Thread
Results 1 to 4 of 4

Copy a first sheet of a workbook in the first sheets of other workbooks

Hybrid View

khracking Copy a first sheet of a... 09-28-2012, 03:12 PM
patel45 Re: Copy a first sheet of a... 09-29-2012, 05:55 AM
Fettertiger Re: Copy the first worksheet... 09-29-2012, 03:36 PM
JBeaucaire Re: Copy a first sheet of a... 09-29-2012, 05:10 PM
  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    Abidjan; Côte d'Ivoire
    MS-Off Ver
    Excel 2010
    Posts
    1

    Copy a first sheet of a workbook in the first sheets of other workbooks

    How can I Copy the first worksheet of one open worbook in the first worksheet of several other workbooks that are closed. Example : copy sheet 1 of workbook 1 in the first sheets of workbook 2, workbook 3, workbook4, etc , all them are closed

    How can I do by code vba

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Copy a first sheet of a workbook in the first sheets of other workbooks

    try this code
    Sub copySheet1ToAllWbInFolder()
        Dim FileName, ws As Worksheet, destWB As Workbook
        Dim pPath As String
        pPath = "D:\DATI\prova"
        Set Sh1 = ActiveWorkbook.Sheets(1)
        With CreateObject("scripting.filesystemobject")
           For Each FileName In .getfolder(pPath).Files
              With Workbooks.Open(FileName)
                 Sh1.Cells.Copy .Worksheets.Add(after:=.Sheets(.Sheets.Count)).Range("A1")
    '            ActiveSheet.Name = "???" 'Name sheet
                .Close True
              End With
            Next
        End With
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    06-09-2011
    Location
    Germany
    MS-Off Ver
    Excel 2016
    Posts
    194

    Re: Copy the first worksheet of one worbook in the first worksheet of several other workbo

    Hi khracking,

    unfortunately you did not write how the macro should know which files need to be opened. I have therefore assumes there is a separate worksheet (called "Files") which has the path (ending with a \) in column A and the filename in column B.

    Option Explicit
    
    Sub Copy_data_to_workbooks()
        Dim i As Integer
        Dim lastrow As Integer
        Dim xw As Workbook
        Dim xs As Worksheet
    
        lastrow = tbl_files.UsedRange.SpecialCells(xlCellTypeLastCell).Row
        For i = 2 To lastrow    'loop through all rows on file table starting in row 2
            On Error Resume Next
            Set xw = Worksheets("Files").Cells(i, 2).Value    'filename of targetfile taken from Files Table
            If Not xw Is Nothing Then    'Check if targetfile is already open
                On Error GoTo 0
                xw.Activate
            Else    'if not open then open file
                On Error GoTo 0
                Workbooks.Open Worksheets("Files").Cells(i, 1).Value & Worksheets("Files").Cells(i, 2).Value
                Set xw = ActiveWorkbook
            End If
    
            xw.Sheets(1).Activate
            Set xs = ActiveSheet
            ThisWorkbook.Sheets(1).Cells.Copy Destination:=xs.Range("A1")    'copy all contents of sheet1 to targetfile
            xw.Close True    'close targetfile and save
            Set xw = Nothing
        Next i
        Set xw = Nothing
        Set xs = Nothing
    End Sub
    Please let me (and the other forum members) know if this works as expected - and don't forget to close the thread if it does.
    Attached Files Attached Files
    Last edited by Fettertiger; 09-29-2012 at 05:39 PM.
    Remember To Do the Following....
    1. Upload sample files
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy a first sheet of a workbook in the first sheets of other workbooks

    Your two separate threads have been merged. Do not post the same question multiple times, it leads to wasting people's time, and I'm sure that's not what you want to happen. Take a moment to read the Forum Rules. Thanks.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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