How to extract data from table in other worksheet

C

Crazy

Hi,

I have one table with autofilter, and all I need is a button to copy the
first line of autofilter result in other worksheet, to be able to print data
I wish out.

problem is if i make macro with record, it will copy only the line I
selected, and autofilter keeps the same line numbers, so it will allways
copy the same I selected, and not the one I filtered.

pls....help.

with thanks
 
D

Dave Peterson

One way:

Option Explicit
Sub testme()
Dim RngToCopy As Range
Dim DestCell As Range

With Worksheets("sheet1")
With .AutoFilter.Range
If .Columns(1).Cells.SpecialCells(xlCellTypeVisible).Count = 1 Then
MsgBox "only the headers are visible"
Exit Sub
End If
'resize to ignore the header
'and come down one row (with the .offset())
Set RngToCopy = .Resize(.Rows.Count - 1).Offset(1, 0) _

..Cells.SpecialCells(xlCellTypeVisible).Rows(1)
End With
End With

With Worksheets("sheet2")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

RngToCopy.Copy _
Destination:=DestCell

End Sub
 
D

Dave Peterson

Watch out for wrapping text...

The code didn't change, but the formatting did.

Option Explicit
Sub testme()
Dim RngToCopy As Range
Dim DestCell As Range

With Worksheets("sheet1")
With .AutoFilter.Range
If .Columns(1).Cells.SpecialCells(xlCellTypeVisible).Count = 1 Then
MsgBox "only the headers are visible"
Exit Sub
End If
'resize to avoid the header
'and come down one row
Set RngToCopy = .Resize(.Rows.Count - 1).Offset(1, 0) _
.Cells.SpecialCells(xlCellTypeVisible).Rows(1)
End With
End With

With Worksheets("sheet2")
Set DestCell = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
End With

RngToCopy.Copy _
Destination:=DestCell

End Sub


Dave Peterson wrote:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top