将可变名称的工作表从一个工作簿复制到另一个工作簿
我正在使用几年前发现的将一个工作表复制到新工作簿的代码,但它使用的cells.copy删除了一些重要的格式。我想使用sheets.copy来代替,但表名不断变化,我不知道如何编码。谢谢你的帮助。这是我目前使用的代码:将可变名称的工作表从一个工作簿复制到另一个工作簿
Sub SheetsToFiles()
'Takes a sheet from a workbook and turns it into a file named after the
sheet name
Dim mySourceWB As Workbook
Dim mySourceSheet As Worksheet
Dim myDestWB As Workbook
Dim myNewFileName As String
' First capture current workbook and worksheet
Set mySourceWB = ActiveWorkbook
Set mySourceSheet = ActiveSheet
' Build new file name based
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"
' Add new workbook and save with name of sheet from other file
Workbooks.Add
ActiveWorkbook.SaveAs Filename:=myNewFileName
Set myDestWB = ActiveWorkbook
' Copy over sheet from previous file
mySourceWB.Activate
Cells.Copy
myDestWB.Activate
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWindow.DisplayGridlines = False
' Resave new workbook
ActiveWorkbook.Save
' Close active workbook
ActiveWorkbook.Close
End Sub
我会用Worksheet.copy
方法将工作表复制到新的工作簿,这应该保存的格式原单。这里的代码更新与评论:
Sub SheetsToFiles()
'Takes a sheet from a workbook and turns it into a file named after the Sheet Name
Dim mySourceWB As Workbook
Dim mySourceSheet As Worksheet
Dim myDestWB As Workbook
Dim myNewFileName As String
' First capture current workbook and worksheet
Set mySourceWB = ActiveWorkbook
Set mySourceSheet = ActiveSheet
' Build new file name based
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"
' Create a new Workbook with one blank Worksheet (this will be deleted later)
Set myDestWB = Workbooks.Add(xlWBATWorksheet)
' Copy sheet to DestWB and paste after the first Worksheet
mySourceSheet.Copy After:=myDestWB.Worksheets(1)
' Delete the unused Worksheet, turn off alerts to bypass the confirmation box
Application.DisplayAlerts = False
myDestWB.Worksheets(1).Delete
Application.DisplayAlerts = True
' Save with name of sheet from other file
myDestWB.SaveAs Filename:=myNewFileName
' Close Destination workbook
myDestWB.Close
End Sub
没有probs刚刚注意到这是你的第一个堆栈溢出问题(恭喜!:D)如果答案很有用,你可以点击勾号框将答案标记为正确旁边的帖子?https://stackoverflow.com/help/someone-answers。干杯。 – Socii
试试这个代码,
Sub SheetsToFiles()
'Takes a sheet from a workbook and turns it into a file named after the
Dim mySourceWB As Workbook
Dim mySourceSheet As Worksheet
Dim myDestWB As Workbook
Dim myNewFileName As String
' First capture current workbook and worksheet
Set mySourceWB = ActiveWorkbook
Set mySourceSheet = ActiveSheet
' Build new file name based
myNewFileName = mySourceWB.Path & "\" & mySourceSheet.Name & ".xlsx"
' Add new workbook and save with name of sheet from other file
Workbooks.Add
Set myDestWB = ActiveWorkbook
myDestWB.SaveAs Filename:=myNewFileName
' Copy over sheet from previous file
mySourceSheet.Range("A1:Z100").Copy Destination:=myDestWB.Sheets("Sheet1").Range("A1:Z100")
ActiveWindow.DisplayGridlines = False
' Resave new workbook
ActiveWorkbook.Save
' Close active workbook
ActiveWorkbook.Close
End Sub
删除所有格式:( –
调整代码内部的范围,它应该粘贴整个范围 –
即使我改变了范围,所有列的宽度也会被重新分组,并且范围也会改变每个工作表并不是所有的报告都是一样的 –
是否有任何模式的表名? –
不是。他们是项目标题。 –