+ Reply to Thread
Results 1 to 9 of 9

moving/copying sheets from different workbooks

Hybrid View

  1. #1
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    moving/copying sheets from different workbooks

    Hi,

    I am trying to create a macro on a template workbook that will open a specific file in the directory and then select the specific sheet name and copy it to the template and then close. For some reason, I am only able to get it to open the specific file. All the code after won't run and I'm not getting any errors. I am probably making a rookie mistake. Can someone please correct it? Thanks in advance.


    Sub importfiles()
    
    
    Dim wbname As String, dt As String
    Dim newwb As Workbook
    
    Application.ScreenUpdating = False
    
    
        
        wbnam = "\\filepath\"
        dt = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
        
       
        Workbooks.Open Filename:=wbnam & "Bank 01" & dt & ".xls"
        
        
        Set newwb= Workbooks("Bank 01" & dt)
        newwb.Sheets("Credit Card").Copy After:=Workbooks("Template.xlsm" _
           ).Sheets(1)
     
         newwb.Close False
        
        End Sub

  2. #2
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: moving/copying sheets from different workbooks

    Hi,
    I am already using following codes...
    Check if they are useful for you or not.

    Sub Macro1()
    ChDrive "C:\"
    ChDir "C:\Users\PC\Downloads\"
    TargetFolder = "C:\BIST\HamDosyalar\" & Format(Now, "yyyymmdd hhmm") & "\"
    MkDir TargetFolder
    x = Application.GetOpenFilename(FileFilter:="Zip Files (*.zip), *.zip", _
                                    FilterIndex:=1, _
                                    Title:="Select files...", _
                                    ButtonText:="", _
                                    MultiSelect:=True)
    If IsArray(x) = True Then
        Set oApp = CreateObject("Shell.Application")
        For i = LBound(x) To UBound(x)
            oApp.Namespace(TargetFolder).CopyHere oApp.Namespace(x(i)).Items
        Next i
    End If
    End Sub
    Sub Macro2()
    ChDrive "C:\"
    ChDir TargetFolder
    Dim wb As Workbook
    Z = Application.GetOpenFilename(FileFilter:="", _
                                    FilterIndex:=1, _
                                    Title:="Select files...", _
                                    ButtonText:="", _
                                    MultiSelect:=True)
    If IsArray(Z) = True Then
        For i = LBound(Z) To UBound(Z)
        Set wb = Workbooks.Open(Z(i), Local:=True)
            wb.Worksheets.Copy Before:=ThisWorkbook.Sheets(1)
            wb.Close True
        Next i
    End If
    End Sub
    Sub DontForgetThese()
         If Your thread includes any code Then Please use code tags...
         If Your thread has been solved Then Please mark as solved...
         If Anybody has helped to you Then Please add reputation...
    End Sub

  3. #3
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: moving/copying sheets from different workbooks

    I am getting an error on the GetOpenFilename line probably cause I'm not inputting my details properly.

    ChDrive "C:\"
    ChDir "filepath"
    Dim wb As Workbook
    Dim dt As String
    
    dt = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
    
    Z = Application.GetOpenFilename(FileFilter:="Bank 01" & dt, _
                                    FilterIndex:=1, _
                                    Title:="Credit Card", _
                                    ButtonText:="", _
                                    MultiSelect:=True)
    If IsArray(Z) = True Then
        For i = LBound(Z) To UBound(Z)
        Set wb = Workbooks.Open(Z(i), Local:=True)
            wb.Worksheets.Copy Before:=Workbooks("Template").Sheets(1)
            wb.Close True
        Next i
    End If
    End Sub

  4. #4
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: moving/copying sheets from different workbooks

    Hi,
    1- Put enclosed file named "Template" to your desktop.
    2- Put enclosed file named "Bank 010814" to under C:
    3- Open "Template" file and run following code.
    Sub importfiles()
    Dim wbname As String
    Dim dt As String
    Dim newwb As Workbook
    wbnam = "C:\"
    dt = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmyy")
    Workbooks.Open Filename:=wbnam & "Bank 01" & dt & ".xlsx"
    Set newwb = Workbooks("Bank 01" & dt)
    newwb.Sheets("Credit Card").Copy After:=Workbooks("Template.xlsm").Sheets(1)
    newwb.Close False
    End Sub
    Regards.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: moving/copying sheets from different workbooks

    Thank you so much for your quick response and I've used your attached files and it worked perfectly. For some reason, when I put it in my original workbook, I still get an error on the Set newwb line though. Is there anything that will cause this?

  6. #6
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: moving/copying sheets from different workbooks

    Change this line;
    Workbooks.Open Filename:=wbnam & "Bank 01" & dt & ".xlsx"
    to this;
    Workbooks.Open Filename:=wbnam & "Bank 01" & dt & ".xls"
    Let me know if it is okey now...

  7. #7
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: moving/copying sheets from different workbooks

    That worked, thanks for picking up the error!!

  8. #8
    Forum Contributor HerryMarkowitz's Avatar
    Join Date
    09-10-2012
    Location
    Europe
    MS-Off Ver
    Office 2021 - Win10
    Posts
    1,014

    Re: moving/copying sheets from different workbooks

    Dont forget reputation
    Dont forget signed as solved...
    Last edited by HerryMarkowitz; 09-05-2014 at 10:03 AM.

  9. #9
    Registered User
    Join Date
    05-23-2014
    Posts
    37

    Re: moving/copying sheets from different workbooks

    Done! I hope I did both correctly

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copying or moving sheets between workbooks
    By Oldgold in forum Excel General
    Replies: 1
    Last Post: 08-28-2008, 08:54 AM
  2. Copying/Moving a Cell(s) Between Sheets/Workbooks in Excel 2007
    By ExcelTip in forum Tips and Tutorials
    Replies: 0
    Last Post: 10-31-2007, 04:32 AM
  3. Copying Sheets, Moving to End
    By MitchU in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-04-2007, 04:37 PM
  4. [SOLVED] Error moving sheets between workbooks
    By Keith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2006, 03:50 AM
  5. Moving multipule sheets into their own workbooks
    By Kathy@nospam.com in forum Excel General
    Replies: 1
    Last Post: 03-31-2005, 05:06 PM

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