2017-10-17 9 views
0

私は現在、ワークブックを新しいブックにコピーするために数年前に見つかったコードを使用していますが、代わりにsheets.copyを使用したいと思いますが、シート名は常に変化しています。どのようにコード化するのかは分かりません。ご協力いただきありがとうございます。ここで私が現在使用しているコードです:1つのブックから別のワークブックに可変名のワークシートをコピーする

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 
+0

シート名にパターンはありますか? –

+0

いいえ、プロジェクトタイトルです。 –

答えて

0

私は新しいワークブックにワークシートをコピーする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 
+0

これはあなたの最初のスタックオーバーフローに関する質問です(おめでとうございます:D)。投稿の隣に?https://stackoverflow.com/help/someone-answers。乾杯。 – Socii

0

このコードを試してみてください、

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 
+0

すべての書式が削除されます:( –

+0

コード内の範囲を調整すると、範囲全体が貼り付けられるはずです –

+0

範囲を変更してもグルーピングが解除され、すべての列幅がリセットされます。 –

関連する問題