Hi
I am writing a code to split a spreadsheet into multiple sheets. I need help with the following issues
1. The headers for the table appear in Row 8 however there is other data from A1:J7 that I would like to appear above as well in the split sheets. At the minute only the table headers A8:J8 appear. The values in cells A1:J7 can be copied and pasted to each sheet.
2. In Cell C4 there is a numerical value. For each sheet that it splits "DAILY PARTS" into I would like this value to increase by 1 for each of them. E.g. if the original C4 says 300 for the first new sheet it reads 301, for sheet 2 it reads 302 etc. After creating the sheets I would like the original to change to whatever the last newly created sheet read. E.g. if there were 4 new cells they would read 301, 302, 303, 304. The original would then change to 304.
3. I would like the new sheets to be renamed as the new value in C4, a space and then the value they are being filtered by
4. Last problem, when I run the current code it creates a sheet first of the headers in Cells A8:J8 with the sheet name of cell J8. Can't work out why that's happening.
Thanks
Daniel
Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
Application.ScreenUpdating = False
vcol = Application.InputBox(prompt:="Which column would you like to filter by?", title:="Filter column", Default:="10", Type:=1)
Set ws = ActiveSheet
lr = ws.Cells(ws.Rows.count, vcol).End(xlUp).Row
title = "A8"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Next
ws.AutoFilterMode = False
ws.Activate
Application.ScreenUpdating = True
End Sub
I am writing a code to split a spreadsheet into multiple sheets. I need help with the following issues
1. The headers for the table appear in Row 8 however there is other data from A1:J7 that I would like to appear above as well in the split sheets. At the minute only the table headers A8:J8 appear. The values in cells A1:J7 can be copied and pasted to each sheet.
2. In Cell C4 there is a numerical value. For each sheet that it splits "DAILY PARTS" into I would like this value to increase by 1 for each of them. E.g. if the original C4 says 300 for the first new sheet it reads 301, for sheet 2 it reads 302 etc. After creating the sheets I would like the original to change to whatever the last newly created sheet read. E.g. if there were 4 new cells they would read 301, 302, 303, 304. The original would then change to 304.
3. I would like the new sheets to be renamed as the new value in C4, a space and then the value they are being filtered by
4. Last problem, when I run the current code it creates a sheet first of the headers in Cells A8:J8 with the sheet name of cell J8. Can't work out why that's happening.
Thanks
Daniel
Sub parse_data()
Dim lr As Long
Dim ws As Worksheet
Dim vcol, i As Integer
Dim icol As Long
Dim myarr As Variant
Dim title As String
Dim titlerow As Integer
Application.ScreenUpdating = False
vcol = Application.InputBox(prompt:="Which column would you like to filter by?", title:="Filter column", Default:="10", Type:=1)
Set ws = ActiveSheet
lr = ws.Cells(ws.Rows.count, vcol).End(xlUp).Row
title = "A8"
titlerow = ws.Range(title).Cells(1).Row
icol = ws.Columns.count
ws.Cells(1, icol) = "Unique"
For i = 2 To lr
On Error Resume Next
If ws.Cells(i, vcol) <> "" And Application.WorksheetFunction.Match(ws.Cells(i, vcol), ws.Columns(icol), 0) = 0 Then
ws.Cells(ws.Rows.count, icol).End(xlUp).Offset(1) = ws.Cells(i, vcol)
End If
Next
myarr = Application.WorksheetFunction.Transpose(ws.Columns(icol).SpecialCells(xlCellTypeConstants))
ws.Columns(icol).Clear
For i = 2 To UBound(myarr)
ws.Range(title).AutoFilter field:=vcol, Criteria1:=myarr(i) & ""
If Not Evaluate("=ISREF('" & myarr(i) & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.count)).Name = myarr(i) & ""
Else
Sheets(myarr(i) & "").Move after:=Worksheets(Worksheets.count)
End If
ws.Range("A" & titlerow & ":A" & lr).EntireRow.Copy Sheets(myarr(i) & "").Range("A1")
Next
ws.AutoFilterMode = False
ws.Activate
Application.ScreenUpdating = True
End Sub
Last edited: