Results 1 to 5 of 5

Creating a Macro to Copy a sheet to several workbooks

Threaded View

  1. #1
    Registered User
    Join Date
    05-18-2016
    Location
    UK
    MS-Off Ver
    2007
    Posts
    4

    Creating a Macro to Copy a sheet to several workbooks

    Hi,

    I am fairly new to the whole Macro scene and need some help please. I am looking for a macro that will copy a sheet from 1 workbook and copy it into several closed workbooks, all saved in the same folder. I have been playing with the below but can't seem to get it to work. Any help greatly appreciated.



    Sub CopySheet()
        
        Const strFldrPath As String = "C:\Workbook Problems\" 'Where the workbooks are all saved
        
        Dim CurrentFile As String, FileExt As String, wb As Workbook, wsActive As Worksheet, ThisExt As String
        Set wsActive = ActiveWorkbook.ActiveSheet
        If InStr(1, ActiveWorkbook.Name, ".", vbTextCompare) > 0 Then
            ThisExt = StrReverse(Left(StrReverse(ActiveWorkbook.Name), InStr(1, StrReverse(ActiveWorkbook.Name), ".", vbTextCompare)))
        Else
            ThisExt = ".xlsx"
        End If
        
        CurrentFile = Dir(strFldrPath)
        While CurrentFile <> vbNullString
            FileExt = StrReverse(Left(StrReverse(CurrentFile), InStr(1, StrReverse(CurrentFile), ".", vbTextCompare)))
            If LCase(ThisExt) = ".xls" Then
                If LCase(FileExt) = ".xls" Or LCase(FileExt) = ".xlsx" Or LCase(FileExt) = ".xlsm" Then
                    Set wb = Workbooks.Open(Filename:=strFldrPath & CurrentFile)
                    wsActive.Copy Before:=wb.Sheets(1)
                    wb.Close True
                End If
            Else
                If LCase(FileExt) = ".xlsx" Or LCase(FileExt) = ".xlsm" Then
                    Set wb = Workbooks.Open(Filename:=strFldrPath & CurrentFile)
                    wsActive.Copy Before:=wb.Sheets(1)
                    wb.Close True
                End If
            End If
            CurrentFile = Dir()
        Wend
        
        ActiveWorkbook.Save
        ActiveWorkbook.Close
       
    End Sub
    Last edited by jeffreybrown; 05-18-2016 at 07:22 AM. Reason: Please use code tags when posting code

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to copy 3 shifts from 8 machines for 18 months, creating a new sheet for each mach.
    By Indychuk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-12-2014, 10:43 AM
  2. If Sheet Exists Stop Macro from Creating a Copy
    By uberathlete in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2014, 09:47 PM
  3. Replies: 3
    Last Post: 09-03-2013, 10:02 PM
  4. Macro to copy values from specific columns from different workbooks to one sheet
    By sksajid7 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-11-2013, 09:50 AM
  5. [SOLVED] Macro copy data from multiple workbooks to master sheet
    By visha_1984 in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 02-08-2013, 10:15 AM
  6. Replies: 3
    Last Post: 10-10-2011, 02:52 PM
  7. [SOLVED] Run Macro & Rename Sheet upon creating copy from blank.
    By Kervin Manasseh in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2005, 07:05 AM

Tags for this Thread

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