I am trying to split one, long worksheet with tables of variable height, but fixed length onto separate worksheets for each table, if possible, without the cell size change.
This is how it looks like, I marked how i need these to be separated. They aren't actually Excel tables, and I cannot alter the original file structure. I need it to be a "just works" macro.
They all start with a "Kod:XXXX" cell and a "Suma:" cell, I figured these could do as separators of where to start and where to end.
This is the code I found, it works fine, but requires the user to pick headers, content and split row manually, one by one.
I Figured out how it should (probably) be structured, but I do not know how to do certain things.
Sub Podziel_na_arkusze()
Dim WorkRng As Range
Dim xRow As Range
Dim SplitRow As Integer
Dim xWs As Worksheet
Dim xTRg As Range
Dim xNTRg As Range
Dim xIER
On Error Resume Next
'All this are things I do not want; I need it to be as automatic as possible
' xTitleId = "Podziel na arkusze"
' Set WorkRng = Application.Selection
' Set xTRg = Application.InputBox("Please select the header row:", xTitleId, "", Type:=8)
' If TypeName(xTRg) = "Nothing" Then Exit Sub
' Set WorkRng = Application.InputBox("Please select the data range(exclude the header 'row):", xTitleId, WorkRng.Address, Type:=8)
' If TypeName(WorkRng) = "Nothing" Then Exit Sub
' SplitRow = Application.InputBox("Split Row Num", xTitleId, Type:=1)
' If SplitRow = 0 Then Exit Sub
' I left stuff I can't figure out empty
Set WorkRng = ("(cell left to Kod cell):(2 cells below "Tel:")
Set xTRg = ("(the "Kod" cell):(the "Suma" cell)")
Set SplitRow = "(any cell of each table, it doesn't matter since they are horizontal)
Set xWs = WorkRng.Parent
Set xRow = WorkRng.Rows(1)
xIER = WorkRng.Rows.Count
xIER = WorkRng.Row + xIER - 1
Application.ScreenUpdating = False
For i = 1 To WorkRng.Rows.Count Step SplitRow
resizeCount = SplitRow
If (xIER - xRow.Row + 1) < SplitRow Then
resizeCount = (xIER - xRow.Row + 1)
End If
xRow.Resize(resizeCount).Copy
Set xWs = Application.Worksheets.Add(after:=Application.Worksheets(Application.Worksheets.Count))
If xIER > (xRow.Row + SplitRow - 1) Then
xWs.Name = xRow.Row & " - " & (xRow.Row + SplitRow - 1)
ElseIf xIER = xRow.Row Then
xWs.Name = xRow.Row
Else
xWs.Name = xRow.Row & " - " & xIER
End If
Application.ActiveSheet.Range("A1").PasteSpecial
Set xNTRg = Application.ActiveSheet.Range("A1")
xTRg.Copy
xNTRg.Insert
Set xRow = xRow.Offset(SplitRow)
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub
How would I get it to automatically detect cells with "kod" and "suma" in them, and loop it till last table has been separated? (and, if possible, make it so cell sizes stay the same)