Hi!

I have a sheet ("FQHCs Overall Measure Summary"), which I don't want to touch at all since it is already designed outside of this code. The data to be filtered and copied to all the empty sheets should be coming from sheet called "Temp".


The problem I'm having with my code to copy range from one sheet to multiple depending on change of value in cell, is that, it is copying all the data into that first sheet ("FQHCs Overall Measure Summary") that I don't want to touch and overriding a row. I, then have to delete via code all the extra rows there that do not belong to that sheet, but already missing data from when the copying occurred.

The second problem is that instead of creating the exact number of extra sheets I need, it is duplicating it, so after everything is copied, I'm deleting the sheets with no data.



Can someone point out what is messed up with my below code? This is only for the filtering and copying and pasting part.


Thanks a lot in advanced.

Tammy

Code:
       Dim r As Excel.Range, r1 As Excel.Range, r2 As Excel.Range
        Dim c2 As Excel.Range
        Dim wks1 As Excel.Worksheet = xlWorkBookFQHCs.Worksheets("Sheet2")
        'Dim wks2 As Excel.Worksheet
        Dim wks3 As Excel.Worksheet = xlWorkBookFQHCs.Worksheets("FQHCs Overall Measure Summary")
        Dim shtTemp As Excel.Worksheet
        Dim shtTemp2 As Excel.Worksheet
        Dim lastrowfq As Long


        wks1.Activate()
        wks1.Name = "Temp"
        r = xlWorkBookFQHCs.Sheets("Temp").Range(xlWorkBookFQHCs.Sheets("Temp").Range("A1"), xlWorkBookFQHCs.Sheets("Temp").Range("A1").End(Excel.XlDirection.xlDown))
        r1 = xlWorkBookFQHCs.Sheets("Temp").Range("A1").End(Excel.XlDirection.xlDown).Offset(1, 0)
        xlWorkBookFQHCs.Sheets("Temp").AutoFilterMode = False
        r.AdvancedFilter(Action:=Excel.XlFilterAction.xlFilterCopy, CopyToRange:=r1, Unique:=True)
        r2 = xlWorkBookFQHCs.Sheets("Temp").Range(r1.Offset(1, 0), r1.End(Excel.XlDirection.xlDown))


        If Not wks1.Name = "FQHCs Overall Measure Summary" Then
            For Each c2 In r2

                For intCount = 1 To 2
                    wks3 = xlWorkBookFQHCs.Worksheets.Add(After:=xlWorkBookFQHCs.Worksheets(intCount))
                    r.CurrentRegion.AutoFilter(Field:=1, Criteria1:=c2.Value)
                    r.CurrentRegion.Cells.SpecialCells(Excel.XlCellType.xlCellTypeVisible).Copy()
                    xlWorkBookFQHCs.Worksheets(intCount).Cells(wks1.Rows.Count, "A").End(Excel.XlDirection.xlUp).Offset(0, 0).PasteSpecial()

                    wks1.Activate()
                    wks1.AutoFilterMode = False

                Next
            Next c2

        End If