gpt4 book ai didi

excel - 无意中创建的新工作簿

转载 作者:行者123 更新时间:2023-12-02 23:05:47 24 4
gpt4 key购买 nike

我正在尝试从工作簿(“InventoryControlSystemV1.1”)复制工作表(“ReceivingRecords”)并将其粘贴到新工作簿(“RecordBook”)中。我创建了一个名为“Temp.xls”的临时工作簿,它允许我使用 SaveCopyAs 方法创建新工作簿“RecordBook”。

当我运行该过程时,“RecordBook”按预期创建,但单元格 A1 中只有一个条目(文本“InventoryControlSystemV1.1.xls”)。

然后将我要复制的工作表粘贴到新的未命名工作簿中。我无法弄清楚在何处或为何创建这个新工作簿。

以下是此过程的代码:

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

最佳答案

替换

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

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

应该可以了。

关于excel - 无意中创建的新工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com