我正在尝试在另一个文件中创建Excel工作表的精确副本。我尝试过以下三种方法
$A$1
VBA
PowerQuery
但是,每个都有其自身的局限性
VBA
- 这两个文件都需要打开,这在我们的情况下是不可能的comments
中创建的
source file
我该怎么办? 我有一个截止日期,非常感谢任何指导。 预先感谢。
使用 VBA,您可以将工作表从一个文件复制到另一个文件。 这对我来说是这样的 按 Alt+F11
Sub CopySheetToAnotherWorkbook()
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceSheet As Worksheet
Dim targetSheet As Worksheet
' Update the paths and sheet names
Dim sourceFilePath As String
Dim targetFilePath As String
Dim sourceSheetName As String
Dim targetSheetName As String
sourceFilePath = "C:\Path\To\SourceWorkbook.xlsx"
targetFilePath = "C:\Path\To\TargetWorkbook.xlsx"
sourceSheetName = "Sheet1"
targetSheetName = "CopiedSheet"
' Open the source and target workbooks
Set sourceWorkbook = Workbooks.Open(sourceFilePath)
Set targetWorkbook = Workbooks.Open(targetFilePath)
' Set the source sheet
Set sourceSheet = sourceWorkbook.Sheets(sourceSheetName)
' Check if target sheet already exists and delete it if it does
On Error Resume Next
Set targetSheet = targetWorkbook.Sheets(targetSheetName)
If Not targetSheet Is Nothing Then
Application.DisplayAlerts = False
targetSheet.Delete
Application.DisplayAlerts = True
End If
On Error GoTo 0
' Copy the sheet to the target workbook
sourceSheet.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
' Rename the copied sheet
Set targetSheet = targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
targetSheet.Name = targetSheetName
' Save and close the workbooks
targetWorkbook.Save
sourceWorkbook.Close False
targetWorkbook.Close True
MsgBox "Sheet copied successfully!"
End Sub
确保输入正确的路径和工作表名称