Consulting

Results 1 to 5 of 5

Thread: how to copy the visible cells of a table to another tab?

  1. #1

    how to copy the visible cells of a table to another tab?

    I need to copy the visible cells of a table to another tab?

    Sub CopyTableFiltered()    Dim DtFirst As Long, DtLast As Long    DtInic = Range("J1").Value
        DtFinal = Range("K1").Value
        ActiveWorkbook.Sheets("Orig").ListObjects("Tabela1").Range.AutoFilter field:=1, Criteria1:=">=" & DtFirst, _
        Operator:=xlAnd, _
        Criteria2:="<=" & DtLast
        'I need copy visible data in table to another worksheet ("Dest"). End Sub
    Cross-Post
    http://www.mrexcel.com/forum/excel-q...other-tab.html

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    ActiveWorkbook.Sheets("Orig").ListObjects("Tabela1").Range.SpecialCells(xlVisible).Copy Sheets("Sheet9").Range("A1")
    Though you should ensure there's no confusion in variable names: DtFirst v. DtInic and DtLast v. DtFinal.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    first thank you very much!

    I would like to ask one more question, how to avoid copying the last row of the table?


    look at my file and understood
    Attached Files Attached Files

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Sub CopyTableFiltered()
    Dim DtFirst As Long, DtLast As Long
    DtFirst = Sheets("Orig").Range("J1").Value
    DtLast = Sheets("Orig").Range("K1").Value
    With ActiveWorkbook.Worksheets("Orig").ListObjects("Tabela1")
      .Range.AutoFilter field:=1, Criteria1:=">=" & DtFirst, Operator:=xlAnd, Criteria2:="<=" & DtLast
      Union(.HeaderRowRange, .DataBodyRange.SpecialCells(xlVisible)).Copy Sheets("Dest").Range("A1")
    End With
    End Sub

    edit post posting:

    a more robust version:
    Sub CopyTableFiltered()
    Dim DtFirst As Long, DtLast As Long, RngToCopy As Range
    DtFirst = Sheets("Orig").Range("J1").Value
    DtLast = Sheets("Orig").Range("K1").Value
    With ActiveWorkbook.Worksheets("Orig").ListObjects("Tabela1")
      .Range.AutoFilter field:=1, Criteria1:=">=" & DtFirst, Operator:=xlAnd, Criteria2:="<=" & DtLast
      On Error Resume Next
      Set RngToCopy = Union(.HeaderRowRange, .DataBodyRange.SpecialCells(xlVisible))
      On Error GoTo 0
      If Not RngToCopy Is Nothing Then
        RngToCopy.Copy Sheets("Dest").Range("A1")
      Else
        MsgBox "No records found"
      End If
    End With
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Hi p45cal, i like it!

    Very good!!

    thank you very much!!!!!!!!!!!!!!

Posting Permissions

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