D3allamerican07
Board Regular
- Joined
- Jul 22, 2015
- Messages
- 101
I have created a code to create a new sheet for every unique value in column A. I'm looking for every time it creates a new sheet, it also brings the same header in Row 1 with it (there are 13 different cells for row 1, A:M)
This is the code I have so far:
Sub Test()
Dim i As Integer
Dim l As Integer
Dim i2 As Integer
Dim l2 As Integer
Dim maxIt As Integer
maxIt = 10 'Set max length of main sheet
Dim mainSheet As String
mainSheet = "Sheet1" 'Set main sheet name
Dim lastColumn As String
lastColumn = "M" 'Set last column of main sheet
Dim exitString As String
exitString = "" 'If you want to end on a specific value in column A
Sheets(mainSheet).Activate
For i = 1 To maxIt
If Sheets(mainSheet).Range("$A$" & i).Value = exitString Then
l = i - 1
Exit For
End If
Next i
For i = 1 To l
Sheets(mainSheet).Range("A" & i & ":" & lastColumn & i).Select
Selection.Copy
On Error Resume Next
Sheets(Sheets(mainSheet).Range("A" & i).Value).Activate
If ActiveSheet.Name <> Sheets(mainSheet).Range("A" & i).Value Then
Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Sheets(mainSheet).Range("A" & i).Value
End If
For i2 = 1 To maxIt
If ActiveSheet.Range("$A$" & i2).Value = "" Then
l2 = i2
Exit For
End If
Next i2
ActiveSheet.Range("$A$" & i2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(mainSheet).Select
Application.CutCopyMode = False
Next i
Sheets(mainSheet).Activate
ActiveSheet.Range("$A$1").Select
End Sub[/B]
This is the code I have so far:
Sub Test()
Dim i As Integer
Dim l As Integer
Dim i2 As Integer
Dim l2 As Integer
Dim maxIt As Integer
maxIt = 10 'Set max length of main sheet
Dim mainSheet As String
mainSheet = "Sheet1" 'Set main sheet name
Dim lastColumn As String
lastColumn = "M" 'Set last column of main sheet
Dim exitString As String
exitString = "" 'If you want to end on a specific value in column A
Sheets(mainSheet).Activate
For i = 1 To maxIt
If Sheets(mainSheet).Range("$A$" & i).Value = exitString Then
l = i - 1
Exit For
End If
Next i
For i = 1 To l
Sheets(mainSheet).Range("A" & i & ":" & lastColumn & i).Select
Selection.Copy
On Error Resume Next
Sheets(Sheets(mainSheet).Range("A" & i).Value).Activate
If ActiveSheet.Name <> Sheets(mainSheet).Range("A" & i).Value Then
Sheets.Add After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = Sheets(mainSheet).Range("A" & i).Value
End If
For i2 = 1 To maxIt
If ActiveSheet.Range("$A$" & i2).Value = "" Then
l2 = i2
Exit For
End If
Next i2
ActiveSheet.Range("$A$" & i2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets(mainSheet).Select
Application.CutCopyMode = False
Next i
Sheets(mainSheet).Activate
ActiveSheet.Range("$A$1").Select
End Sub[/B]