+ Reply to Thread
Results 1 to 4 of 4

Thread: copy data if it exists

  1. #1
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    copy data if it exists

    I don't understand why my macro is failing.

    I'm pulling the detail from a pivot table, then filtering out certain criteria. I want to copy anything under the headers, but not the headers themselves. If the available info is ONLY the headers, I don't want to copy anything. My code is breaking down when only headers are available. If I run the code below, the answer that come back is 1 (since only the headers are visible)
    Dim rng As Range
    Set rng = ActiveSheet.UsedRange
    
    MsgBox rng.Columns(1).SpecialCells(xlCellTypeVisible).Count
    So, my code to run is set up below. However, for the "only headers" range (1 above), my code fails on the COPY line, which I don't think should even be read since 1 is not greater than 1. What do I have wrong?
    Set rng = ActiveSheet.UsedRange
                If Columns(1).SpecialCells(xlCellTypeVisible).Count > 1 Then
                'copy the detail to Today's sheet
                ActiveSheet.UsedRange.Offset(1, 0).Resize(ActiveSheet.UsedRange.Rows.Count - 1).EntireRow.Copy Destination:=Exp.Sheets("Today").Range("A65536").End(xlUp).Offset(1, 0)
                Else
                End If

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,374

    Re: copy data if it exists

    This is a similar code that i use, which works. Maybe you can adapt your code accordingly.

    With ActiveSheet
    
        Set rngF = .AutoFilter.Range
        If rngF.Columns(1).Cells.SpecialCells(xlCellTypeVisible) _
                        .Cells.Count = 1 Then
    
              MsgBox "No records available" 'This is optional
              Exit Sub
        
        End If
    
    End With
    
    With rngF  'ignore the header from the count and come down one row
    
            Set rngV = .Resize(.Rows.Count - 1, 1).Offset(1, 0).Cells.SpecialCells(xlCellTypeVisible)
    
    End With
    
    rngV.Next.EntireRow.Select 'use your copy code here

  3. #3
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: copy data if it exists

    No need for testing (nor for variables) : if no cells match the criterion/criteria nothing will be 'copied'

    Sub snb()
     With Sheets(1).Cells(1).CurrentRegion
      .AutoFilter 1, "vv"
      .Offset(1).Copy Cells(1, 10)
      .AutoFilter
     End With
    End Sub



  4. #4
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: copy data if it exists

    Sorry for never respondingto this one. My problem was due to an error elsewhere in my code, but I thank you both for your help, and to SNB for providing an alternative code to copying lines. Today was the first chance I've had to take a look at it.

    Thanks,
    John

+ 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.2.0