【问题标题】:Creating a workbook then adding sheets from another workbook创建工作簿,然后从另一个工作簿添加工作表
【发布时间】:2019-12-04 09:35:23
【问题描述】:

我需要一个可以创建具有明确路径、名称和格式的 excel 文件的 VBA 代码,所以我编写了这些代码行:

Sub Parametrage()
'This is the workbook i need to copy sheets from , i chose to store those sheets in variable because i may need them later 
Workbooks.Open "C:\Users\Oumayna EL JAHRANI\Desktop\Test\Fichier Type.xlsx"
Dim ClasseurType As Workbook: Set ClasseurType = ActiveWorkbook
Dim Display As Worksheet: Set Publié = ClasseurType.Worksheets("Publié")
Dim Ajustements As Worksheet: Set Ajustement = ClasseurType.Worksheets("Ajustement")
Dim Variables As Worksheet: Set Variables = ClasseurType.Worksheets("Variables")
Dim Database As Worksheet: Set Source = ClasseurType.Worksheets("Source")
'Here starts the creation of the new file 
Dim CibleApp As New Excel.Application
Dim CibleClasseur As Workbook
Set CibleClasseur = CibleApp.Workbooks.Add
 With CibleClasseur
 .Title = "Classeur Cible"
 .Subject = "Cible"
 .SaveAs Filename:="C:\Users\Oumayna EL JAHRANI\Desktop\Test\ObjetCible.xlsx"
 End With
'Here i add the sheets i want to my new workbook and this is the part i can't get to execute 
Sheets(Publié).Copy Before:=Workbooks(CibleClasseur).Sheets(1)
Sheets(Ajustement).Copy Before:=Workbooks(CibleClasseur).Sheets(2)
Sheets(Variables).Copy Before:=Workbooks(CibleClasseur).Sheets(3)
Sheets(Source).Copy Before:=Workbooks(CibleClasseur).Sheets(5)
Workbooks(ClasseurType).Close savechanges:=True
Application.Quit
End Sub 

这段代码有两个问题,首先当我执行它时它在第 16 行停止,第二个问题是因为它每次执行第二次时都会创建一个 xlsx 文件,所以我在该行出现错误我保存新文件的地方,之后我不能删除旧文件。我希望首先能够执行整个代码并能够删除文件并在代码停止时关闭应用程序。

【问题讨论】:

  • 为什么要使用单独的 Excel 实例?这就是阻止您复制工作表的原因。
  • 我认为有必要创建一个新文件,删除这部分可以更轻松地删除新的excel文件,但它仍然不允许复制工作表
  • 你是对的,但我的文件中已经有一张工作表,所以我猜这个副本会添加一个新工作表,这就是为什么第二个工作表是 sheet(2) 等等。,有吗添加工作表的任何方法?
  • 我没有说任何关于文件的事情。您正在启动一个完全独立的 Excel 应用程序 实例,并且不能在不同的应用程序实例之间复制工作表。我的问题是你为什么要这样做?
  • 因为我认为如果你没有一个新的应用程序你不能创建一个新文件,我知道这是错误的谢谢你。所以现在我知道这是该部分的新代码行Dim CibleClasseur As Workbook Set CibleClasseur = Workbooks.Add With CibleClasseur .Title = "Classeur Cible" .Subject = "Cible" .SaveAs Filename:="C:\Users\Oumayna EL JAHRANI\Desktop\Test\ObjetCible.xlsx" End With 但我的代码仍然没有复制表格。

标签: excel vba copy add spreadsheet


【解决方案1】:

我相信你的代码应该是这样的:

Sub Parametrage()
'This is the workbook i need to copy sheets from , i chose to store those sheets in variable because i may need them later

Dim ClasseurType As Workbook: Set ClasseurType = Workbooks.Open("C:\Users\Oumayna EL JAHRANI\Desktop\Test\Fichier Type.xlsx")
Dim Publié As Worksheet: Set Publié = ClasseurType.Worksheets("Publié")
Dim Ajustement As Worksheet: Set Ajustement = ClasseurType.Worksheets("Ajustement")
Dim Variables As Worksheet: Set Variables = ClasseurType.Worksheets("Variables")
Dim Source As Worksheet: Set Source = ClasseurType.Worksheets("Source")
'Here starts the creation of the new file
Dim CibleClasseur As Workbook
Set CibleClasseur = Workbooks.Add
 With CibleClasseur
 .Title = "Classeur Cible"
 .Subject = "Cible"
 .SaveAs Filename:="C:\Users\Oumayna EL JAHRANI\Desktop\Test\ObjetCible.xlsx"
 End With
'Here i add the sheets i want to my new workbook and this is the part i can't get to execute
Publié.Copy Before:=CibleClasseur.Sheets(1)
Ajustement.Copy Before:=CibleClasseur.Sheets(2)
Variables.Copy Before:=CibleClasseur.Sheets(3)
Source.Copy Before:=CibleClasseur.Sheets(5)
ClasseurType.Close savechanges:=True

End Sub

我不清楚您最后打算关闭哪个工作簿。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2013-08-16
    • 1970-01-01
    • 2021-06-16
    • 2013-10-23
    • 1970-01-01
    相关资源
    最近更新 更多