K
K
Kotar4ik2020-11-17 13:55:36
excel
Kotar4ik, 2020-11-17 13:55:36

How to save file keeping page title in Excel using VBA?

Using the following code in VBA, I split the entire file into n files with the specified number of lines. The problem is that all generated files do not accept the page name of the source file. Please help, how can I rewrite the script so that all new files have the same page name as the original file?

Option Explicit
Option Base 1 

Sub Border_Limit()
  Dim Limit As Integer, Count As Integer, SaveDir As String, SetTitle As Boolean
  
  Count = 1: Limit = 1000 
  SetTitle = True
  
  SaveDir = ThisWorkbook.Path 
  While Not IsEmpty(Cells(IIf(SetTitle, 2, 1), 1))
    Rows("1:" & Limit).Copy
    Workbooks.Add xlWBATWorksheet 
    ActiveSheet.Paste: Cells(1, 1).Select
    ActiveWorkbook.SaveAs Filename:=SaveDir & "\file_" & Count & ".xlsx", _
      FileFormat:=xlOpenXMLWorkbook
    ActiveWindow.Close
    Rows(IIf(SetTitle, 2, 1) & ":" & Limit).Delete Shift:=xlUp
    Count = Count + 1
  Wend: MsgBox "File separated on " & Count - 1 & " files. "
End Sub

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis, 2020-11-17
@Kotar4ik

After Workbooks.Add xlWBATWorksheet
insert
ActiveSheet.Name = ThisWorkbook.ActiveSheet.Name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question