Hi, I wonder whether someone may be able to help me please.
Using a post which I found here http://www.mrexcel.com/forum/excel-q...worksheet.html
I've put together a very simple script shown (below) which creates sheets with the sheet name derived from cell values within a given range. The script then copies additional data from the 'Source' sheet and pastes this into the applicable 'Destination' sheet.
Sub NewWorksheetForEachDept()
Dim WBO As Workbook
Dim ThisWS
Dim rngFilter As Range 'filter range
Dim rngUniques As Range 'Unique Range
Dim cell As Range
Dim counter As Integer
Dim rngResults As Range 'filter range
Dim LastRow As Long
Dim Values As Range
Dim iX As Integer
Set WBO = ThisWorkbook
Set rngFilter = Range("P4", Range("P" & Rows.Count).End(xlUp))
Set rngResults = Range("A1", Range("O" & Rows.Count).End(xlUp))
With rngFilter
.AdvancedFilter Action:=xlFilterInPlace, Unique:=True
Set rngUniques = Range("P5", Range("P" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
End With
For Each cell In rngUniques
Worksheets.Add After:=Worksheets(Worksheets.Count)
ThisWS = cell.Value
ActiveSheet.Name = ThisWS
'counter = counter + 1
rngFilter.AutoFilter Field:=1, Criteria1:=cell.Value
rngResults.SpecialCells(xlCellTypeVisible).Copy Destination:=WBO.Sheets(ThisWS).Range("A1")
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
If LastRow >= StartRow Then
With Range("B5:O" & LastRow)
.Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 2), order2:=xlAscending
End With
End If
Next cell
End Sub
The problem I have is that this script runs against the whole workbook, so if there is more than one sheet which contains data when the macro is run, the script naturally has trouble in selecting the correct 'Source' information.
So I know that I need to specify the 'Source' sheet but I'm a little unsure about how to go about it. I just wondered whether someone may be able to look at this please and offer some guidance on how I may change this so that I can specify the 'Source' sheet as "Unique Records" and create the new worksheets in a new workbook whilst still keeping the rest of the current functionality.
Many thanks and kind regards
Bookmarks