Filtering and Pasting Data to new individual Sheets based on field values

pliskers

Active Member
Joined
Sep 26, 2002
Messages
462
Office Version
  1. 2016
Platform
  1. Windows
I'm having a small problem with the code below, which is taking a data range that runs from columns A-AY (beginning in row 1). It's intended to filter on column N, named Route, and paste all rows per route into a separate tab. There are multiple rows containing each route, and the data is sorted by the route column.

The code runs without error, but the data on each tab (while named correctly for each Route) contains the full set of data, not the data filtered on just that route. There must be a small step missing or an incorrect value somewhere.

Would appreciate some assistance with this, please...

VBA Code:
Sub CopyFIlteredData()

Dim r As Integer, route As String, wa As Worksheet
Set ws = ActiveSheet
ws.Range("A1:AY1").AutoFilter
r = 1

Do

r = r + 1
route = ws.Range("N" & r).Value
On Error Resume Next
If Sheets(route) Is Nothing Then
    ws.Range("A1:AY1").AutoFilter Field:=14, Criteria:=route
    ws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy
    Sheets.Add.Name = route
    Sheets(route).Paste
    ws.ShowAllData
End If

Loop While ws.Range("A" & r + 1).Value <> ""
  
  
  
End Sub
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Issue resolved.
Thanks for letting us know. (y)

For the future though, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top