Results 1 to 7 of 7

Open workbook, copy data, save and close error

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-24-2009
    Location
    London, UK
    MS-Off Ver
    Excel 2007
    Posts
    178

    Open workbook, copy data, save and close error

    Hi there,

    I hope someone may be able to help me please?

    I have a workbook (workbook A) with lots of data in.
    A second workbook (workbook B) is to be found in a location which doesn't change, opened, some sheets copied over and for it to be saved as a copy in the same location as workbook A. I have a code to do this and it seemed to work perfectly.

    However I have added a third workbook, workbook C which I want exactly the same thing to happen as with workbook B. The data copied will be slightly different but the process is the same.

    When I use the same code for workbook C I don't get a VBA error but it says the excel needs to close and about an error report - could anyone help me with the reason why please and how I can fix it?

    My current code is below. Many thanks.

    Sub Proceed()
    
    Dim newpathway As String
    
    Application.ScreenUpdating = False
    
    With Sheets("Sheet3")
        .Cells.Copy
        .Cells.PasteSpecial Paste:=xlValues
    End With
    
    On Error Resume Next
    ActiveWorkbook.Names("Name1").Delete
    ActiveWorkbook.Names("Name2").Delete
    ActiveWorkbook.Names("Name3").Delete
    On Error GoTo 0
    
    newpathway = ThisWorkbook.Path
    
    WorkbookB
    
    Workbooks("WorkbookA.xlsm").Activate
    newpathway = ThisWorkbook.Path
    
    WorkbookC
    Workbooks("WorkbookA.xlsm").Activate
    
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Sheet5").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    
    MsgBox ("Please continue in the new workbooks")
    Exit Sub
    
    Application.ScreenUpdating = True
    
    End Sub
    
    Sub WorkbookB()
    
    Dim excelfile As String
    Dim newpathway As String
    
    excelfile = "WorkbookB.xlsm"
    Workbooks.Open "C:\My Documents" & excelfile
    
    Workbooks("WorkbookA.xlsm").Activate
    Sheets(Array("Sheet1", "Sheet3")).Copy After:=Workbooks("WorkbookB.xlsm").Sheets("Front Page")
    
    Workbooks("WorkbookB.xlsm").Activate
    
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveCopyAs Filename:=newpathway & ActiveWorkbook.Name
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    
    End Sub
    
    Sub WorkbookC()
    
    Dim excelfile As String
    Dim newpathway As String
    
    excelfile = "WorkbookC.xlsm"
    Workbooks.Open "C:\My Documents\" & excelfile
    
    Workbooks("WorkbookA.xlsm").Activate
    Sheets(Array("Sheet2", "Sheets3", "Sheet4", "Information")).Copy After:=Workbooks("WorkbookC.xlsm").Sheets("Front Page")
    
    Workbooks("WorkbookC.xlsm").Activate
    
    Application.DisplayAlerts = False
    ActiveWorkbook.SaveCopyAs Filename:=newpathway & ActiveWorkbook.Name
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
    
    End Sub
    Last edited by Jessica.Bush; 01-01-2012 at 12:44 PM.

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