Want to copy only fist and second row after applying autofilter in VBA if data is available after autofilter

Arif_Tamboli

New Member
Joined
Mar 1, 2018
Messages
34
Office Version
  1. 2016
I want to copy only first and second row after applying autofilter in VBA.
My code
row_no = ThisWorkbook.Sheets("Agent List").Range("B" & Application.Rows.Count).End(xlUp).Row
For I = 3 To row_no
AGENTNAME = ThisWorkbook.Sheets("Agent List").Cells(I, 2).Value
ThisWorkbook.Sheets("Main Data").Activate
ThisWorkbook.Sheets("Main Data").Range("A1:J1").AutoFilter Field:=7, Criteria1:=AGENTNAME
On Error Resume Next
below mentioned line code is copying all the visible rows
ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Areas(2).Copy
I don't want that
just want first and second visible row if data is available
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
replace this line

ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Areas(2).Copy

with

Range("A2:J3").copy
 
Upvote 0
Not sure what you mean, do you want the heading and the first visible row or the 2 rows beneath the heading
 
Upvote 0
Not sure what you mean, do you want the heading and the first visible row or the 2 rows beneath the heading

I don't need heading, what I want is
After applying autofilter
if visible rows are more than 2. than I want to copy first two visible row without heading.
if only one row is available still I want to copy it.

But I don't want more than 2 visible rows.
 
Upvote 0
I have looked into this and it involves looping through the visible cells which is a bit of coding I'm unfamiliar with, perhaps someone else can jump in.

a second suggestion and one I could be tempted by is to do the full copy to a different location then take the 2 top rows from the copy.
 
Upvote 0
I have looked into this and it involves looping through the visible cells which is a bit of coding I'm unfamiliar with, perhaps someone else can jump in.

a second suggestion and one I could be tempted by is to do the full copy to a different location then take the 2 top rows from the copy.

A second suggestion, I am doing right now that way but want to modify that and reduce manual work.
Help me on this
 
Upvote 0
I'm trying

you don't need to do it manually. put it somewhere out of the way for instance

ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible).Areas(2).Copy destination:= Range ("A1000000")
range ("A1000000:J1000001").copy
 
Upvote 0
No sure what you mean. Sorry

All this is doing is copying everything to A1000000 then taking the top 2 rows to where ever the macro goes next

You should add the line before end sub: Range("A1000000").currentregion.delete
 
Upvote 0

Forum statistics

Threads
1,215,483
Messages
6,125,064
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top