cortexnotion
Board Regular
- Joined
- Jan 22, 2020
- Messages
- 150
- Office Version
- 2013
- Platform
- Windows
Hi All
I'm splitting a worksheet into number of rows defined by the input box. I would like to keep the Header Row from the original book in each file. Can you help please?
Many thanks
I'm splitting a worksheet into number of rows defined by the input box. I would like to keep the Header Row from the original book in each file. Can you help please?
Many thanks
VBA Code:
Sub Test()
Application.ScreenUpdating = False
Dim wb As Workbook
Dim ThisSheet As Worksheet
Dim NumOfColumns As Integer
Dim RangeToCopy As Range
Dim WorkbookCounter As Integer
Dim RowsInFile As Variant
Dim DMANumber As String
Dim FileName As String
RowsInFile = InputBox("How many rows?")
DMANumber = Range("L3").Value
FileName = "CWDP_DMA-" & DMANumber & "_" & Format(Date, "dd-mm-yyyy") & "-"
Set ThisSheet = ThisWorkbook.ActiveSheet
NumOfColumns = ThisSheet.UsedRange.Columns.Count
WorkbookCounter = 1
For p = 1 To ThisSheet.UsedRange.Rows.Count Step RowsInFile
Set wb = Workbooks.Add
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 1, NumOfColumns))
RangeToCopy.Copy wb.Sheets(1).Range("A1")
wb.SaveAs ThisWorkbook.Path & "\" & FileName & WorkbookCounter
wb.Close
WorkbookCounter = WorkbookCounter + 1
Next p
Application.ScreenUpdating = True
End Sub