pliskers
Active Member
- Joined
- Sep 26, 2002
- Messages
- 462
- Office Version
- 2016
- Platform
- Windows
VBA expert, please help!
Below is some code I've used before but am having trouble adapting to new use. Basically, what I want is the following:
I have a table of sales data (named "Opp_Locator") starting in cell A9 (the header), extending to column AY, and in column N is the name of the salesperson (named "Route"). I would like to use a loop to filter the existing data and create a separate tab in the worksheet for each salesperson, with the salesperson's name from column N pasted into the name of the tab. The new salesperson tabs should ideally be pasted after the one with full details.
The data is already sorted by salesperson, so all of each person's records are sequentially grouped in order.
I was getting error messages trying to modify the code to my current layout, in part because originally the header was in row 1. Again, the table header is in row 9, but I would like the newly created tabs to include the full layout, including what's in rows 1-8.
Can anyone offer some tweaks to modify my code so it works with the data and layout as described.
Thanks a million!
JP
Option Explicit
Sub FilterData()
Dim ws1Master As Worksheet, wsNew As Worksheet, wsFilter As Worksheet
Dim Datarng As Range, FilterRange As Range, objRange As Range
Dim rowcount As Long
Dim colcount As Integer, FilterCol As Integer
Dim SheetName As String
'master sheet
Set ws1Master = ActiveSheet
'set the Column you
'are filtering
top:
On Error Resume Next
Set objRange = Application.InputBox("Select Field Name To Filter", "Range Input", , , , , , 8)
On Error GoTo 0
If objRange Is Nothing Then
Exit Sub
ElseIf objRange.Columns.Count > 1 Then
GoTo top
End If
FilterCol = objRange.Column
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
On Error GoTo progend
'add filter sheet
Set wsFilter = Sheets.Add
With ws1Master
.Activate
.Unprotect Password:="" 'add password if needed
rowcount = .Cells(.Rows.Count, FilterCol).End(xlUp).Row
colcount = .Cells(1, .Columns.Count).End(xlToLeft).Column
If FilterCol > colcount Then
Err.Raise 65000, "", "FilterCol Setting Is Outside Data Range.", "", 0
End If
Set Datarng = .Range(.Cells(1, 1), .Cells(rowcount, colcount))
'extract Unique values from FilterCol
.Range(.Cells(1, FilterCol), _
.Cells(rowcount, _
FilterCol)).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=wsFilter.Range("A1"), _
Unique:=True
rowcount = wsFilter.Cells(wsFilter.Rows.Count, "A").End(xlUp).Row
'set Criteria
wsFilter.Range("B1").Value = wsFilter.Range("A1").Value
For Each FilterRange In wsFilter.Range("A2:A" & rowcount)
'check for blank cell in range
If Len(FilterRange.Value) > 0 Then
'add the FilterRange to criteria
wsFilter.Range("B2").Value = FilterRange.Value
SheetName = RTrim(Left(FilterRange.Value, 31))
'if FilterRange sheet exists
'update it
If SheetExists(SheetName) Then
Sheets(SheetName).Cells.Clear
Else
'add new sheet
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = SheetName
End If
Datarng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=wsFilter.Range("B1:B2"), _
CopyToRange:=Sheets(SheetName).Range("A1"), _
Unique:=False
End If
Next
.Select
End With
progend:
wsFilter.Delete
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
If Err > 0 Then
MsgBox (Error(Err)), vbCritical, "Error"
Err.Clear
End If
End Sub
Below is some code I've used before but am having trouble adapting to new use. Basically, what I want is the following:
I have a table of sales data (named "Opp_Locator") starting in cell A9 (the header), extending to column AY, and in column N is the name of the salesperson (named "Route"). I would like to use a loop to filter the existing data and create a separate tab in the worksheet for each salesperson, with the salesperson's name from column N pasted into the name of the tab. The new salesperson tabs should ideally be pasted after the one with full details.
The data is already sorted by salesperson, so all of each person's records are sequentially grouped in order.
I was getting error messages trying to modify the code to my current layout, in part because originally the header was in row 1. Again, the table header is in row 9, but I would like the newly created tabs to include the full layout, including what's in rows 1-8.
Can anyone offer some tweaks to modify my code so it works with the data and layout as described.
Thanks a million!
JP
Option Explicit
Sub FilterData()
Dim ws1Master As Worksheet, wsNew As Worksheet, wsFilter As Worksheet
Dim Datarng As Range, FilterRange As Range, objRange As Range
Dim rowcount As Long
Dim colcount As Integer, FilterCol As Integer
Dim SheetName As String
'master sheet
Set ws1Master = ActiveSheet
'set the Column you
'are filtering
top:
On Error Resume Next
Set objRange = Application.InputBox("Select Field Name To Filter", "Range Input", , , , , , 8)
On Error GoTo 0
If objRange Is Nothing Then
Exit Sub
ElseIf objRange.Columns.Count > 1 Then
GoTo top
End If
FilterCol = objRange.Column
With Application
.ScreenUpdating = False
.DisplayAlerts = False
End With
On Error GoTo progend
'add filter sheet
Set wsFilter = Sheets.Add
With ws1Master
.Activate
.Unprotect Password:="" 'add password if needed
rowcount = .Cells(.Rows.Count, FilterCol).End(xlUp).Row
colcount = .Cells(1, .Columns.Count).End(xlToLeft).Column
If FilterCol > colcount Then
Err.Raise 65000, "", "FilterCol Setting Is Outside Data Range.", "", 0
End If
Set Datarng = .Range(.Cells(1, 1), .Cells(rowcount, colcount))
'extract Unique values from FilterCol
.Range(.Cells(1, FilterCol), _
.Cells(rowcount, _
FilterCol)).AdvancedFilter _
Action:=xlFilterCopy, _
CopyToRange:=wsFilter.Range("A1"), _
Unique:=True
rowcount = wsFilter.Cells(wsFilter.Rows.Count, "A").End(xlUp).Row
'set Criteria
wsFilter.Range("B1").Value = wsFilter.Range("A1").Value
For Each FilterRange In wsFilter.Range("A2:A" & rowcount)
'check for blank cell in range
If Len(FilterRange.Value) > 0 Then
'add the FilterRange to criteria
wsFilter.Range("B2").Value = FilterRange.Value
SheetName = RTrim(Left(FilterRange.Value, 31))
'if FilterRange sheet exists
'update it
If SheetExists(SheetName) Then
Sheets(SheetName).Cells.Clear
Else
'add new sheet
Set wsNew = Sheets.Add
wsNew.Move After:=Worksheets(Worksheets.Count)
wsNew.Name = SheetName
End If
Datarng.AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=wsFilter.Range("B1:B2"), _
CopyToRange:=Sheets(SheetName).Range("A1"), _
Unique:=False
End If
Next
.Select
End With
progend:
wsFilter.Delete
With Application
.ScreenUpdating = True
.DisplayAlerts = True
End With
If Err > 0 Then
MsgBox (Error(Err)), vbCritical, "Error"
Err.Clear
End If
End Sub