VBA Looping AutoFilter Criteria using Dynamic Array

Jawnne

New Member
Joined
Sep 3, 2014
Messages
31
Hello All,


I am an old user of Excel 2003 with some knowledge of VBA, my new job uses Excel 2010 (a lot has changed since that version I see). I could use some help on how to approach this new problem.


Objective: I want to take a exported report of raw data which lets say has 5 column of pertinent information (Name, Clinic, Appointment Date/Time, Primary Doctor, and Care Team). I want to automate the process of filtering the each Care Team criteria, select all visible cells, copy it to a new worksheet, and use each criteria as the name of the new sheet.


I don't want to use Advanced Filter at this point, I would like to use the AutoFilter, Array_Filter, and a loop if necessary with some explanation of what is going on behind the code that would further my understanding that would be great.


This is what I have pieced together so far:


Sub FilterReport()


Dim sSheet As String
ActiveSheet.UsedRange.Select
Selection.Copy


sSheet = InputBox("What is the name of this Worksheet?", "Name New Worksheet")
intCount = Sheets.Count
Worksheets.Add(After:=Worksheets(intCount)).Name = sSheet
Range("A1").Select
Selection.PasteSpecial _
Paste:=xlPasteColumnWidths, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
ActiveSheet.Paste


End Sub


I found this code in one of my searches, not sure how to incorporate it if at all:


.UsedRange.AutoFilter field:=7, Criteria1:=Array_filter, Operator:=xlFilterValues


Also, in my data I have blanks as in this field which refers to patients not having a Care Team or Doctor. Which at which time I would have to look at their see what doctor they have an appointment with, reference another table to see what Care Team that Doctor is assigned to. This is usually half of the blanks which refers to a patient, the other half are those same patients which are assigned a Social Worker who works for a group of teams which the only way to know what team they are on is to associate the other matching name. All of these I want to be able to paste into the other sheets corresponding to the Care Teams previously or simultaneously created.


Thanks in advance for any help anyone can provide.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The loop works great!! How do I maintain the column widths when the autofilter loop copies to the destination?
 
Upvote 0
Andrew,

This is what I have thus far. The PasteSpecial does not work.

Code:
Sub APTest()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim c As Range
    Dim List As New Collection
    Dim Item As Variant
    Dim ShNew As Worksheet
    Application.ScreenUpdating = False
'   *** Change Sheet name to suit ***
    Set Sh = ActiveSheet
    Set Rng = Sh.Range("A2:A" & Sh.Range("A65536").End(xlUp).Row)
    On Error Resume Next
    For Each c In Rng
        List.Add c.Value, CStr(c.Value)
    Next c
    On Error GoTo 0
    Set Rng = Sh.Range("A1:E" & Sh.Range("A65536").End(xlUp).Row)
    For Each Item In List
        Set ShNew = Worksheets.Add
        ShNew.Name = Item
        Rng.AutoFilter Field:=1, Criteria1:=Item
        Rng.SpecialCells(xlCellTypeVisible).Copy ShNew.Range("A1") '.PasteSpecial xlColumnWidths - compile error
        'ShNew.Range("A1").PasteSpecial Paste:=xlColumnWidths 'Run-time error '1004' PasteSpecial method of Range class failed
        Rng.AutoFilter
    Next Item
    Sh.Activate
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Andrew,

I could not get that to work. I have a more pressing matter from my original post, which is how to deal with the blanks in this field. The blanks represent new patients who have not be seen as of yet. So they do not have a care team or a provider. However, they do have appointments. With the help of my Care Team Assignments table I have been manually moving them to the respective teams. Another issue is the Social Workers work for -

We have three Teams here each is called a different color (Red, White, Blue)
Each Team is sub divided by into Care Teams, which consists of Nurse, Doctor, and Social Worker.
Each Social Worker works for a Team and not for one specific Care Team, so their Care Team assignments are based on the patient's Doctor's and Nurse's Care Team Assignment.

The Problem is that there is only one Social Worker per Team, and the record cannot be associated to the Care Team by using Table 2, only by associating to the patient.

Any thoughts on how I can incorporate this to the existing macro would be greatly appreciated.


Table 1: Raw Data Report

Patient NameClinic/ProviderAppointment Date/TimePrimary DoctorCare Team
Flintstone,FredBlue Team Nurse 209/04/2014 07:00 AMDr. 1Blue 6
Flintstone,FredBlue Team Dr. 109/04/2014 07:30 AMDr. 1Blue 6
Flintstone,WilmaRed Team Nurse 809/04/2014 07:00 AMDr. 2Red 8
Flintstone,WilmaRed Team Dr. 209/04/2014 07:30 AMDr. 2Red 8
Rubble,BarneyBlue Team Nurse 609/04/2014 07:00 AMDr. 3Blue 2
Rubble,BarneyBlue Team Dr. 309/04/2014 07:30 AMDr. 3Blue 2
Rubble,BarneyBlue Team SW 109/04/2014 08:00 AMDr. 3Blue 2
Rubble,BettyWhite Team Nurse 409/04/2014 07:00 AM
Rubble,BettyWhite Team Dr. 409/04/2014 07:30 AM
Rubble,BettyWhite Team SW 209/04/2014 08:00 AM
White,BettyBlue Team Nurse 209/04/2014 08:00 AM
White,BettyBlue Team Dr. 109/04/2014 08:30 AM
White,BettyBlue SW09/04/2014 09:00 AM

<tbody>
</tbody>

Table 2: Care Team Assignments

Provider Name
Care Team
Nurse 2
Blue 6
Nurse 4White 1
Nurse 6Blue 2
Nurse 8Red 8
Dr. 1Blue 6
Dr. 2Red 8
Dr. 3Blue 2
Dr. 4White 1
SW 1Blue

<tbody>
</tbody>
 
Upvote 0
The Hospital has 3 wings each wing is referred to as a Team (Red Team, White Team, and Blue Team).

Each of these Teams is subdivided into Care Teams which consists of a Nurse, Doctor, and Social Worker.

So the Red Team may have 5 Care Teams (Red Team 1, Red Team 2, Red Team 6, Red Team 8 just as an example).

There is only 1 Social Worker per wing so they are referred to by Blue SW, Red SW, and White SW.
The Social Worker does not have its own Care Team but is dependent on who the patient is assigned or in this case scheduled to see.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,228
Members
448,951
Latest member
jennlynn

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