+ Reply to Thread
Results 1 to 13 of 13

Copy and paste data to separate sheets based on mutiple criteria again

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Copy and paste data to separate sheets based on mutiple criteria again

    I had a problem solved for me previously here. However, I now have additional data that needs to be copied over to a new sheet. This data includes blank cells.

    I embedded an If statement to help fix the issue, but it doesn't seem to work.

    For Each iCell In wsData.Range("A" & CurrentItem & ":L" & CurrentItem)
        Dim rngNextLine As Range:   Set rngNextLine = wsDest.Cells(Rows.Count, iCell.Column).End(xlUp).Offset(1, 0)
            If iCell.Value = "" Then
            rngNextLine.Value = ""
            Else
            rngNextLine.Value = iCell.Value
            End If
    Next iCell
    Can anyone tell me what I'm doing wrong? I've attached my sample.
    Attached Files Attached Files
    Last edited by Ricker090; 04-27-2011 at 01:50 PM. Reason: Solved

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Hello Ricker090,

    It would help to know what the expected outcome looks like.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Sorry about that.

    I've attached the expected outcome.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Hello Ricker090,

    Thanks for doing that. I didn't mean for you do that much work. Just a few sheets would have been fine.

  5. #5
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    It wasn't a problem. I actually let the macro create the sheets, I just manually copied and pasted the additional data.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Hello Ricker090,

    Sorry for the delay. I have rewrote the macro to be more flexible and faster. A button has been added to "Sheet1" to run the macro. Here is the revised macro code...
    Sub MoveData_1()
    
      Dim DataTitle As String
      Dim DstWks As Worksheet
      Dim Headers As Range
      Dim NextRow As Range
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = Worksheets("Master Data")
        
        Set Headers = SrcWks.UsedRange.Rows(1)
        
        Set Rng = Headers.Offset(1, 0)
        Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = SrcWks.Range(Rng, RngEnd)
        
        Application.ScreenUpdating = False
        
          For R = 1 To Rng.Rows.Count
            DataTitle = Rng.Item(R, 1) & Rng.Item(R, 2) & Rng.Item(R, 3)
            
            If DataTitle <> SrcWks.Name Then
               On Error Resume Next
               Set DstWks = Worksheets(DataTitle)
                 If Err <> 0 Then
                    Worksheets.Add After:=Worksheets(Worksheets.Count)
                    ActiveSheet.Name = DataTitle
                    Set DstWks = ActiveSheet
                    Err.Clear
                 End If
               On Error GoTo 0
               
               Set NextRow = DstWks.Cells(Rows.Count, "A").End(xlUp)
                 If NextRow.Row = 1 And NextRow = "" Then
                    Headers.Copy NextRow
                 End If
               
               Set NextRow = NextRow.Offset(1, 0)
               Rng.Rows(R).Copy NextRow
            End If
          Next R
        
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Holy Crap! It's awesome. Thanks!

  8. #8
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    I had a quick follow up question. If I wanted to paste my header and subsiquent data starting in row 7, how would I modify the code? I attempted to modify the If statement that pasted the header with the following code, but I takes the next line of data and pastes it into row 1.

               Set NextRow = DstWks.Cells(Rows.Count, "A").End(xlUp)
                If NextRow.Row = 1 And NextRow = "" Then
                    Headers.Copy NextRow(7)
                End If

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Hello Ricker090,

    Change the NextRow number from 1 to 7...
               Set NextRow = DstWks.Cells(Rows.Count, "A").End(xlUp)
                If NextRow.Row = 7 And NextRow = "" Then
                    Headers.Copy NextRow
                End If

  10. #10
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    I tried that and it and doesn't copy the header over. Any other suggestions?

  11. #11
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    Hello Ricker090,

    I changed the macro to start at "A7" on the individual destination sheets. The macro has been tested and works. Replace your old macro with this code.
    Sub MoveData_1a()
    
      Dim DataTitle As String
      Dim DstRng As Range
      Dim DstWks As Worksheet
      Dim Headers As Range
      Dim NextRow As Range
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SrcWks As Worksheet
      
        Set SrcWks = Worksheets("Master Data")
        
        Set Headers = SrcWks.UsedRange.Rows(1)
        
        Set Rng = Headers.Offset(1, 0)
        Set RngEnd = SrcWks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = SrcWks.Range(Rng, RngEnd)
        
        Application.ScreenUpdating = False
        
          For R = 1 To Rng.Rows.Count
            DataTitle = Rng.Item(R, 1) & Rng.Item(R, 2) & Rng.Item(R, 3)
            
            If DataTitle <> SrcWks.Name Then
               On Error Resume Next
               Set DstWks = Worksheets(DataTitle)
                 If Err <> 0 Then
                    Worksheets.Add After:=Worksheets(Worksheets.Count)
                    ActiveSheet.Name = DataTitle
                    Set DstWks = ActiveSheet
                    Err.Clear
                 End If
               On Error GoTo 0
               
               Set DstRng = DstWks.Range("A7")
               Set NextRow = DstWks.Cells(Rows.Count, "A").End(xlUp)
               
                 If NextRow.Row < DstRng.Row Then
                    Set NextRow = DstWks.Range("A7")
                    Headers.Copy NextRow
                 End If
               
               Set NextRow = NextRow.Offset(1, 0)
               Rng.Rows(R).Copy NextRow
            End If
          Next R
        
        Application.ScreenUpdating = True
    
    End Sub

  12. #12
    Forum Contributor
    Join Date
    06-17-2010
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    157

    Re: Copy and paste data to separate sheets based on mutiple criteria again

    That did it! I'm greatful for everything that you've done. Thank you so much.

  13. #13
    Registered User
    Join Date
    07-13-2011
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    1

    Smile Re: Copy and paste data to separate sheets based on mutiple criteria again

    Holy crap...this is amazing!! I modified the VBA code for my purpose and it works great. THANK YOU!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1