Consulting

Results 1 to 2 of 2

Thread: Duplicating Multiple sheets to all workbooks in folder

  1. #1
    VBAX Newbie
    Joined
    Jul 2020
    Posts
    1
    Location

    Duplicating Multiple sheets to all workbooks in folder

    Hello,

    I am trying to copy multiple worksheets from my template to all excel workbooks in a folder. How would I do this by using VBA?

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Use the Dir Function
    Open each book
    Copy the sheets
    Close the Book
    Loop after Next Dir

    From Help File:
    Dir Function Example
    This example uses the Dir function to check if certain files and directories exist. On the Macintosh, “HD:” is the default drive name and portions of the pathname are separated by colons instead of backslashes. Also, the Microsoft Windows wildcard characters are treated as valid file-name characters on the Mac. However, you can use the MacID function to specify file groups.
    Dim MyFile, MyPath, MyName
    ' Returns "WIN.INI" (on Microsoft Windows) if it exists.
    MyFile = Dir("C:\WINDOWS\WIN.INI")    
    
    ' Returns filename with specified extension. If more than one *.ini
    ' file exists, the first file found is returned.
    MyFile = Dir("C:\WINDOWS\*.INI")
    
    ' Call Dir again without arguments to return the next *.INI file in the 
    ' same directory.
    MyFile = Dir
    
    ' Return first *.TXT file with a set hidden attribute.
    MyFile = Dir("*.TXT", vbHidden)
    
    ' Display the names in C:\ that represent directories.
    MyPath = "c:\"    ' Set the path.
    MyName = Dir(MyPath, vbDirectory)    ' Retrieve the first entry.
    Do While MyName <> ""    ' Start the loop.
        ' Ignore the current directory and the encompassing directory.
        If MyName <> "." And MyName <> ".." Then
            ' Use bitwise comparison to make sure MyName is a directory.
            If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
                Debug.Print MyName    ' Display entry only if it
            End If    ' it represents a directory.
        End If
        MyName = Dir    ' Get next entry.
    Loop
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •