+ Reply to Thread
Results 1 to 6 of 6

Copy range from one sheet and paste in another sheet on the next visible row after header

Hybrid View

  1. #1
    Registered User
    Join Date
    05-20-2013
    Location
    Rotterdam, Holland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Copy range from one sheet and paste in another sheet on the next visible row after header

    Good day, hope someone can help me with a challenge I'm facing. I have recently started on VBA and have come across the following problem:

    The VBA code to copy non-hidden range data from one worksheet, and paste to another worksheet is generally working. But the problem I have come across is that the destination sheet
    has a header row and below that nothing but hidden rows. I would like the destination row to paste to to be the first row after the header row, which is basically the first visible row after all the hidden rows. The last row function will get me to the header row, but Lr + 1 is getting Excel/VBA to paste to the next HIDDEN row.

    So far, the code I have is only working when I unhide the rows in the destination sheet first.

    Can Excel/VBA paste to the next visible row??

    Hope to get your solution, if any.

    Best regards, MaQian.

  2. #2
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: Copy range from one sheet and paste in another sheet on the next visible row after hea

    This is assuming the first row is 1 and you're in column A. It will place the active cell below all of the hidden rows. You can then paste after that.
    Range("A1").Select
    ActiveCell.Offset(1, 0).Select
    Do Until ActiveCell.EntireRow.Hidden = False
        ActiveCell.Offset(1, 0).Select
    Loop

  3. #3
    Registered User
    Join Date
    05-20-2013
    Location
    Rotterdam, Holland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Copy range from one sheet and paste in another sheet on the next visible row after hea

    Hi Majosum, thanks for your response. Unf I didn't get it working yet, I must be doing something wrong.

    With the current code it copies the named range data from sheet DailyEnq and pastes it at sheet DailyEmail at Cell H5. Row 4 is where my visible header data is, and below that are quite a lot of hidden rows. Still left with the question on how to get the code to paste to next visible row (in H column), below the visible header row (row 4)?

    For ease of reference I copied the code below. Any suggestions? Much appreciated..

    Sub Copy_Daily_Enqs()
           
       Dim SourceRange As Range, DestRange As Range
        Dim DestSheet As Worksheet
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'fill in the Source Sheet and range
        Set SourceRange = Sheets("DailyEnq").Range("DAILY_ENQ_RANGE3")
    
       
        'Fill in the destination sheet
        Set DestSheet = Sheets("DailyEmail")
        
        'Copy the source range
        
        SourceRange.Copy
           
        'Create a destination cell
        Set DestRange = DestSheet.Range("H4")
        ActiveCell.Offset(1, 0).Select
        Do Until ActiveCell.EntireRow.Hidden = False
            ActiveCell.Offset(1, 0).Select
        Loop
    
        'Use PasteSpecial to paste in the destination cell
        
        DestRange.PasteSpecial _
                Paste:=xlPasteValues, _
                operation:=xlPasteSpecialOperationNone, _
                skipblanks:=False, _
                Transpose:=False
        Application.CutCopyMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Last edited by MaQian; 05-22-2013 at 08:46 AM.

  4. #4
    Forum Contributor
    Join Date
    03-12-2013
    Location
    Chicago, USA
    MS-Off Ver
    Excel 2007
    Posts
    230

    Re: Copy range from one sheet and paste in another sheet on the next visible row after hea

    If you want it in the 5th column instead of column H, set the destination range to be E4 instead of H4.

  5. #5
    Registered User
    Join Date
    05-15-2013
    Location
    Wisconsin, USA
    MS-Off Ver
    Excel 2010
    Posts
    42

    Re: Copy range from one sheet and paste in another sheet on the next visible row after hea

    The reason that it still pastes in H4 is because after you do the loop to find the next hidden cell, you never stored that as DestRange

    replace this
    'Create a destination cell
        Set DestRange = DestSheet.Range("H4")
        ActiveCell.Offset(1, 0).Select
        Do Until ActiveCell.EntireRow.Hidden = False
            ActiveCell.Offset(1, 0).Select
        Loop
    with this
    'Create a destination cell
    Set DestRange = DestSheet.Range("H5")
        Do Until DestRange.EntireRow.Hidden = False
           Set DestRange = DestRange.Offset(1, 0)
        Loop
    Last edited by krabine; 05-22-2013 at 09:29 AM.

  6. #6
    Registered User
    Join Date
    05-20-2013
    Location
    Rotterdam, Holland
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Copy range from one sheet and paste in another sheet on the next visible row after hea

    Thanks guys, great help to finally get it working in the end. Storing the DestRange did the trick, although had a tweak a bit to make sure that Active cell had the right starting point in the right worksheet before going through the loop.

    For completeness, final code below. Thanks again.



    Sub Copy_Daily_Enqs()
           
       Dim SourceRange As Range, DestRange As Range
      
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'fill in the Source Sheet and range
        Set SourceRange = Sheets("DailyEnq").Range("DAILY_ENQ_RANGE3")
    
          
        'Create a destination cell
       Sheets("DailyEmail").Select
       Sheets("DailyEmail").Range("H4").Select
           ActiveCell.Offset(1, 0).Select
        Do Until ActiveCell.EntireRow.Hidden = False
            ActiveCell.Offset(1, 0).Select
            Loop
      Set DestRange = ActiveCell
    'Copy the source range
        
        SourceRange.Copy
        
      'Use PasteSpecial to paste in the destination cell
        
        DestRange.PasteSpecial _
                Paste:=xlPasteValues, _
                operation:=xlPasteSpecialOperationNone, _
                skipblanks:=False, _
                Transpose:=False
        Application.CutCopyMode = False
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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