A new workbook created unintentionally

I am trying to copy a worksheet ("ReceivingRecords") from a tutorial ("InventoryControlSystemV1.1") and paste it into a new book ("BookBook"). I have created a temporary book called "Temp.xls" that allows me to use the SaveCopyAs method to create my new "RecordBook".

When I run the procedure, the "RecordBook" is created as intended, but with only one entry (the text "InventoryControlSystemV1.1.xls") in cell A1.

The worksheet I want to copy is then pasted into a new, unnamed workbook. I cannot understand where and why this new book is being created.

Here is the code for this procedure:

Sub WriteReceivingToRecords()

    Dim UsedRng As Range
    Dim LastCol As Long
    Dim BeginDate, EndDate
    Dim NameString
    Dim FormatBeginDate, FormatEndDate
    Dim BackupQuest As Integer
    Dim BackupMsg As String

    'Confirmation dialog box to avoid mistakes
    BackupMsg = "This will create a new workbook for the period" & vbNewLine
    BackupMsg = BackupMsg & " since the last backup was made, and will clear" & vbNewLine
    BackupMsg = BackupMsg & " the receiving records in this workbook." & vbNewLine & vbNewLine
    BackupMsg = BackupMsg & "Are you sure you want to back up the receiving records?"
    BackupQuest = MsgBox(BackupMsg, vbYesNo, "Back-up Records")

    If BackupQuest = vbNo Then
        Exit Sub
    Else

    '   Find start and end dates of receiving - To use for worksheet title
        Workbooks("InventoryControlSystemV1.1.xls").Activate
        Worksheets("ReceivingRecords").Activate
        Set UsedRng = ActiveSheet.UsedRange
        LastCol = UsedRng(UsedRng.Cells.Count).Column
        Do While Cells(2, LastCol) = ""
                LastCol = LastCol - 1
        Loop
        EndDate = Cells(2, LastCol).Text
        BeginDate = Cells(2, 2).Text

        FormatBeginDate = Format(BeginDate, "d mmmm yy")
        FormatEndDate = Format(EndDate, "d mmmm yy")
        NameString = "M-Props Receiving Records " & FormatBeginDate & " To " _
            & FormatEndDate & ".xls"



        Workbooks("InventoryControlSystemV1.1.xls").Sheets("ReceivingRecords").Copy

        Workbooks.Open Filename:="Temp.xls"
        Workbooks("Temp.xls").Activate
        Workbooks("Temp.xls").Worksheets("Sheet1").Paste _
            Destination:=Workbooks("Temp.xls").Worksheets("Sheet1").Range("A1")

        Workbooks("Temp.xls").SaveCopyAs NameString & ".xls"
        Workbooks("Temp.xls").Close False

    End If

End Sub

      

+3


source to share


1 answer


Replace

Workbooks("InventoryControlSystemV1.1.xls").Sheets("ReceivingRecords").Copy

      

from



Workbooks("InventoryControlSystemV1.1.xls").Sheets("ReceivingRecords").Cells.Copy

      

This should do it.

0


source







All Articles