Solved

VBA to autofilter in a specific column for each criteria and copy the result to a new sheet

Posted on 2016-09-04
3
52 Views
Last Modified: 2016-09-04
Need to filter on each region (Column F) and copy the result to a new worksheet. would be helpful if this can be done through looping as the original data set contains more regions. Thanks for the help
https://drive.google.com/file/d/0B02...5nbDlvME0/view

thanks
0
Comment
Question by:kishore naidu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41783847
Your file link is broken. Why didn't you upload the file here itself?

Anyways please find attached a sample workbook with some dummy data along with a button on Sheet1 and code on Module1.
Please click the button to run the code. The code will copy the data for all the regions to their respective sheets.
Sub FilterByRegionAndCopyData()
Dim sws As Worksheet, dws As Worksheet
Dim dict, it, x
Dim lr As Long, i As Long
Application.ScreenUpdating = False
Set sws = Sheets("Sheet1")  'Sheet which contains data for all the regions
lr = sws.Cells(Rows.Count, 6).End(xlUp).Row
x = sws.Range("F2:F" & lr).Value
Set dict = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(x, 1)
   dict.Item(x(i, 1)) = ""
Next i
For Each it In dict.keys
   With sws.Range("A1").CurrentRegion
      .AutoFilter field:=6, Criteria1:=it
      On Error Resume Next
      Set dws = Sheets(it)
      dws.Cells.Clear
      On Error GoTo 0
      If dws Is Nothing Then
         Sheets.Add(after:=Sheets(Sheets.Count)).Name = it
         Set dws = ActiveSheet
      End If
      sws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy dws.Range("A1")
      .AutoFilter
   End With
   Set dws = Nothing
Next it
Application.ScreenUpdating = False
MsgBox "Data for all the regions has been copied to their respective region sheet successfully.", vbInformation, "Done!"
End Sub

Open in new window

FilterAndCopyData.xlsm
0
 

Author Closing Comment

by:kishore naidu
ID: 41783863
Thanks a lot. This is more than what i expected.i shall change the code to suit to the actual data.
0
 
LVL 31

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41783884
You're welcome Kishore! Glad to help.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being an active EE Expert means to get a lot of (E)EMail, as you certainly know. If you are using Outlook, I'll show you how to minimize your inbox contents without losing anything – even improve the experience by changing the Subject line to facili…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question