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
39 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
  • 2
3 Comments
 
LVL 30

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 30

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is an Add-On procedure to be used in conjunction with the code provided in Reducing EE Email Clutter using Outlook (http://www.experts-exchange.com/Software/Office_Productivity/Groupware/Outlook/A_3146-Outlook-Processing-EE-emails-on-Receive.…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

790 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