DanG
Registered User.
- Local time
- Yesterday, 18:41
- Joined
- Nov 4, 2004
- Messages
- 477
Hello,
I have a some code from the following site that keeps giving me a "type mismatch" error. I have used this code with sucess quite a while ago and don't remember having this sort of problem.
The code is supposed to look at column A and based on unique values make new sheets within the workbook for each unique value. It doesn't mater what data I put in the cells, I get the error. I even just used one column with a header and 4 sample values under the header as follows:
Name
Dan
Dan
Bil
Jim
Here is the code:
Any help would be great!
I have a some code from the following site that keeps giving me a "type mismatch" error. I have used this code with sucess quite a while ago and don't remember having this sort of problem.
The code is supposed to look at column A and based on unique values make new sheets within the workbook for each unique value. It doesn't mater what data I put in the cells, I get the error. I even just used one column with a header and 4 sample values under the header as follows:
Name
Dan
Dan
Bil
Jim
Here is the code:
Code:
Sub LoopingSheetName()
'By Thomas Urtis
'Prepare Excel
Application.ScreenUpdating = False
ActiveSheet.AutoFilterMode = False
'Declare your one worksheet as a source sheet variable
Dim SourceSheet As Worksheet
Set SourceSheet = Worksheets("Sheet1")
'Make sure you start from the source sheet (modify name if needed)
SourceSheet.Activate
'Declare filter range variable, from column A to column E (modify column range as needed)
Dim FilterRange As Range
Set FilterRange = Range(("A1"), Cells(Rows.Count, 1).End(xlUp).Offset(0, 4))
'Sort the range in ascending order so we do not add sheets for duplicate names
FilterRange.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
'Start in cell A2
Range("A2").Activate
'Loop down column A to determine identical values (names).
'Filter each unique name set.
'Create a new sheet and copy each filtered recordset to its respective new sheet
Do
If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
ActiveCell.Offset(1, 0).Activate
Else
FilterRange.AutoFilter Field:=1, Criteria1:=ActiveCell.Value
SourceSheet.Range("Z1").Formula = "=RIGHT(FilterCriteria(Sheet1!R2C1),LEN(FilterCriteria(Sheet1!R2C1))-SEARCH(""="",FilterCriteria(Sheet1!R2C1)))"
Dim NewSheetName As String
NewSheetName = SourceSheet.Range("Z1").Value
With Worksheets.Add(after:=Sheets(Sheets.Count))
Application.DisplayAlerts = False
On Error Resume Next
Worksheets(NewSheetName).Delete
On Error GoTo 0
Application.DisplayAlerts = True
.Name = NewSheetName
SourceSheet.Activate
FilterRange.SpecialCells(xlCellTypeVisible).Copy Worksheets(NewSheetName).Range("A1")
SourceSheet.AutoFilterMode = False
End With
ActiveCell.Offset(1, 0).Activate
End If
Loop While Len(ActiveCell) <> 0
'Re-set Excel
Range("Z1").ClearContents
Application.Goto Range("A1"), True
Application.ScreenUpdating = True
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''
Function FilterCriteria(Rng As Range) As String
'By Stephen Bullen
Dim Filter As String
Filter = ""
On Error GoTo Finish
With Rng.Parent.AutoFilter
If Intersect(Rng, .Range) Is Nothing Then GoTo Finish
With .Filters(Rng.Column - .Range.Column + 1)
If Not .On Then GoTo Finish
Filter = .Criteria1
Select Case .Operator
Case xlAnd
Filter = Filter & " AND " & .Criteria2
Case xlOr
Filter = Filter & " OR " & .Criteria2
End Select
End With
End With
Finish:
FilterCriteria = Filter
End Function
Any help would be great!