dcsimg
Results 1 to 3 of 3

Thread: Copying data from one sheet to multiple depending on cell is overlapping data

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    58

    Unhappy Copying data from one sheet to multiple depending on cell is overlapping data

    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

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,372

    Re: Copying data from one sheet to multiple depending on cell is overlapping data

    For intCount = 1 To 2
    wks3 = xlWorkBookFQHCs.Worksheets.Add(After:=xlWorkBookFQHCs.Worksheets(intCount))
    for each cell in r2 add worksheet for 1 to 2 times, i would assume this is part of your problem

    xlWorkBookFQHCs.Worksheets(intCount).Cells(wks1.Rows.Count, "A").End(Excel.XlDirection.xlUp).Offset(0, 0).PasteSpecial()
    this pastes to sheets 1 then sheets 2 in loop
    assuming sheets 1 is the one you do not want to paste to, and the recently added sheet is the one you want to paste to, try
    Code:
    wks3.cells(1, 1).PasteSpecial()
    i may be mistaken about the target of your pasting
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    58

    Re: Copying data from one sheet to multiple depending on cell is overlapping data

    Hi westconn1!

    I fixed my issue by using the following code, which is doing exactly what I wanted it to do:

    Code:
            Dim lastrowsheets As Long
            Dim lastrow As Long, LastCol As Integer, iqn As Long, iStart As Long, iEnd As Long
            Dim wks1 As Excel.Worksheet
            Dim wks2 As Excel.Worksheet = xlWorkBookFQHCs.Sheets("Sheet2")
    
            With wks2
                lastrow = .Cells(.Rows.Count, "A").End(Excel.XlDirection.xlUp).Row
                LastCol = .Cells(1, .Columns.Count).End(Excel.XlDirection.xlToLeft).Column
                iStart = 2
                For iqn = 2 To lastrow
                    If .Range("A" & iqn).Value <> .Range("A" & iqn + 1).Value Then
                        iEnd = iqn
                        xlWorkBookFQHCs.Sheets.Add(After:=xlWorkBookFQHCs.Sheets(xlWorkBookFQHCs.Sheets.Count))
                        wks1 = xlWorkBookFQHCs.ActiveSheet
    
                        wks1.Name = .Range("A" & iStart).Value
    
                        wks1.Range(wks1.Cells(1, 1), wks1.Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
                        .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy(Destination:=wks1.Range("A2"))
                        iStart = iEnd + 1
                    End If
                Next iqn
            End With
            xlAppFQHCs.Application.CutCopyMode = False

    Thanks for your help though!

    Tammy

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width