Alejandro_R
New Member
- Joined
- Nov 10, 2014
- Messages
- 1
Hi All,
I am trying to add two pieces of coding to a coding that I found online at Create a new sheet for all Unique values and which what it does is create and name worksheets based on a list located in ‘Sheet1’ Column A and copy the cells from the same row of the list to the newly created sheet. The coding I am trying to add are: one that duplicate (makes copies) of a Worksheet ‘Template’ (instead of creating blank worksheets), and another coding that copies the row of information shown in the ‘Sheet1’ to column H of the created worksheet (in specific cells of column H, all cells not necessarily contiguous).
This is:
The first script I am trying to add is:
Sheets("Template").Cells.Copy
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
The second script I am trying to add is:
Range("B?").Copy Sheets("Sheet1").Range("H81")
Range("C?:E?").Copy Sheets("Sheet1").Range("H94:H96")
I am sorry about writing “?”, I am not sure how to express that the particular row may be located between row 2 and 200. The total number of rows may increase up to less than 1000.
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
'WORKING ON THIS FROM HERE
'first copy the data in worksheet "Template" to ws2
'Sheets("Template").Cells.Copy
' ActiveSheet.Paste
'UNTIL HERE WORKING ON THIS
'OR SOMETHING LIKE THIS
'Sheets("Template").Cells.Copy
'ActiveSheet.Paste
'Range("A1").Select
'Application.CutCopyMode = False
'UNTIL HERE SOMETHING LIKE THIS
'AND ADDING SOMETHING LIKE:
'Range("B?").Copy Sheets("Sheet1").Range("H81")
'Range("C?:E?").Copy Sheets("Sheet1").Range("H94:H96")
'UNTIL HERE
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Even though the original script allows copying more than one row and pasting them in ws2, as if it were the case that there were repetitive names in Column A, that will not be the case here, there will only be one (non-repetitive) name per worksheet and I suppose there should be no problem of copying one row of values of Sheet1 and pasting them in column H. Therefore I think it will be okay that I don’t take the time cleaning the coding for the moment.
This coding is updatable, but if I add new rows of information to the list and run the macro again, it will show the repetitive worksheets named “Error-“number”” plus the missing worksheets. Honestly, if solving the code for this is complicated, I don’t mind deleting these worksheets.
Can anyone help me with this? I am new in VBA and very slowly learning this fascinating language.
Thank you in advance for the help.
I am trying to add two pieces of coding to a coding that I found online at Create a new sheet for all Unique values and which what it does is create and name worksheets based on a list located in ‘Sheet1’ Column A and copy the cells from the same row of the list to the newly created sheet. The coding I am trying to add are: one that duplicate (makes copies) of a Worksheet ‘Template’ (instead of creating blank worksheets), and another coding that copies the row of information shown in the ‘Sheet1’ to column H of the created worksheet (in specific cells of column H, all cells not necessarily contiguous).
This is:
- Template to copy is ‘Template’ Worksheet and contains data and formulae in basically the range of A1:AU295
- List of data to copy are in ‘Sheet1’ from Range A2:E200, and their headings are in Range A1:E1
- The values assigned for naming the worksheets are in column A by using a filter field. No data is repetitive in this field.
- The unique data from the filter field (column A to E) is copied to the newly created worksheet (ws2)
- One value in Column B will be pasted in “H81” of ws2
- The value in Columns C,D,E will be pasted in “H94:H96” of ws2
- I will later add more columns (F through L) which I will assign corresponding ranges, all in column H (some non-consecutive and some consecutive cells in column H)
The first script I am trying to add is:
Sheets("Template").Cells.Copy
ActiveSheet.Paste
Range("A1").Select
Application.CutCopyMode = False
The second script I am trying to add is:
Range("B?").Copy Sheets("Sheet1").Range("H81")
Range("C?:E?").Copy Sheets("Sheet1").Range("H94:H96")
I am sorry about writing “?”, I am not sure how to express that the particular row may be located between row 2 and 200. The total number of rows may increase up to less than 1000.
Sub Copy_To_Worksheets()
'Note: This macro use the function LastRow
Dim My_Range As Range
Dim FieldNum As Long
Dim CalcMode As Long
Dim ViewMode As Long
Dim ws2 As Worksheet
Dim Lrow As Long
Dim cell As Range
Dim CCount As Long
Dim WSNew As Worksheet
Dim ErrNum As Long
'Set filter range on ActiveSheet: A1 is the top left cell of your filter range
'and the header of the first column, D is the last column in the filter range.
'You can also add the sheet name to the code like this :
'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1")))
'No need that the sheet is active then when you run the macro when you use this.
Set My_Range = Range("A1:D" & LastRow(ActiveSheet))
My_Range.Parent.Select
If ActiveWorkbook.ProtectStructure = True Or _
My_Range.Parent.ProtectContents = True Then
MsgBox "Sorry, not working when the workbook or worksheet is protected", _
vbOKOnly, "Copy to new worksheet"
Exit Sub
End If
'This example filters on the first column in the range(change the field if needed)
'In this case the range starts in A so Field:=1 is column A, 2 = column B, ......
FieldNum = 1
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
'Change ScreenUpdating, Calculation, EnableEvents, ....
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
ViewMode = ActiveWindow.View
ActiveWindow.View = xlNormalView
ActiveSheet.DisplayPageBreaks = False
'Add a worksheet to copy the a unique list and add the CriteriaRange
Set ws2 = Worksheets.Add
'WORKING ON THIS FROM HERE
'first copy the data in worksheet "Template" to ws2
'Sheets("Template").Cells.Copy
' ActiveSheet.Paste
'UNTIL HERE WORKING ON THIS
'OR SOMETHING LIKE THIS
'Sheets("Template").Cells.Copy
'ActiveSheet.Paste
'Range("A1").Select
'Application.CutCopyMode = False
'UNTIL HERE SOMETHING LIKE THIS
'AND ADDING SOMETHING LIKE:
'Range("B?").Copy Sheets("Sheet1").Range("H81")
'Range("C?:E?").Copy Sheets("Sheet1").Range("H94:H96")
'UNTIL HERE
With ws2
'first we copy the Unique data from the filter field to ws2
My_Range.Columns(FieldNum).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=.Range("A1"), Unique:=True
'loop through the unique list in ws2 and filter/copy to a new sheet
Lrow = .Cells(Rows.Count, "A").End(xlUp).Row
For Each cell In .Range("A2:A" & Lrow)
'Filter the range
My_Range.AutoFilter Field:=FieldNum, Criteria1:="=" & _
Replace(Replace(Replace(cell.Value, "~", "~~"), "*", "~*"), "?", "~?")
'Check if there are no more then 8192 areas(limit of areas)
CCount = 0
On Error Resume Next
CCount = My_Range.Columns(1).SpecialCells(xlCellTypeVisible) _
.Areas(1).Cells.Count
On Error GoTo 0
If CCount = 0 Then
MsgBox "There are more than 8192 areas for the value : " & cell.Value _
& vbNewLine & "It is not possible to copy the visible data." _
& vbNewLine & "Tip: Sort your data before you use this macro.", _
vbOKOnly, "Split in worksheets"
Else
'Add a new worksheet
Set WSNew = Worksheets.Add(After:=Sheets(Sheets.Count))
On Error Resume Next
WSNew.Name = cell.Value
If Err.Number > 0 Then
ErrNum = ErrNum + 1
WSNew.Name = "Error_" & Format(ErrNum, "0000")
Err.Clear
End If
On Error GoTo 0
'Copy the visible data to the new worksheet
My_Range.SpecialCells(xlCellTypeVisible).Copy
With WSNew.Range("A1")
' Paste:=8 will copy the columnwidth in Excel 2000 and higher
' Remove this line if you use Excel 97
.PasteSpecial Paste:=8
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
.Select
End With
End If
'Show all data in the range
My_Range.AutoFilter Field:=FieldNum
Next cell
'Delete the ws2 sheet
On Error Resume Next
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
On Error GoTo 0
End With
'Turn off AutoFilter
My_Range.Parent.AutoFilterMode = False
If ErrNum > 0 Then
MsgBox "Rename every WorkSheet name that start with ""Error_"" manually" _
& vbNewLine & "There are characters in the name that are not allowed" _
& vbNewLine & "in a sheet name or the worksheet already exist."
End If
'Restore ScreenUpdating, Calculation, EnableEvents, ....
My_Range.Parent.Select
ActiveWindow.View = ViewMode
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Even though the original script allows copying more than one row and pasting them in ws2, as if it were the case that there were repetitive names in Column A, that will not be the case here, there will only be one (non-repetitive) name per worksheet and I suppose there should be no problem of copying one row of values of Sheet1 and pasting them in column H. Therefore I think it will be okay that I don’t take the time cleaning the coding for the moment.
This coding is updatable, but if I add new rows of information to the list and run the macro again, it will show the repetitive worksheets named “Error-“number”” plus the missing worksheets. Honestly, if solving the code for this is complicated, I don’t mind deleting these worksheets.
Can anyone help me with this? I am new in VBA and very slowly learning this fascinating language.
Thank you in advance for the help.