How to copy only required numbers of rows from filtered column as per criteria to another sheet -Audit sample

hellodj143

New Member
Joined
Sep 3, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi Dear, I am new to vba and need your help. I have a data of 5000 rows(dynamic). i want to pick audit sample(not randomly, from top to bottom) per director. i need only 10 sample out of 5000 or whatever rows are. Also criteria is that i have 6-7 Director (column name) and at least one rows from all the director but total number should not be 10 rows.
Data is available on Temp1 sheet and i want to copy to Sample_new sheet for each director one transaction only and if total number is less than 10 than can pick sample 2 as well from some director.
Example: I have 1000 rows, and 7 directors in a column, so my sample will be picked 1 from each director and for remaining 3 loop will continue and pick again 1 from any of three director. so i will have 6 rows from 3 director and 4 rows from 4 director.

Note: Attached image is just a sample not the exact as per my coding due to privacy.

VBA Code:
VBA Code:
Sub copy1strows()
Dim i As Integer
'Dim j As Integer
'Dim k As Integer

 Dim s1 As Worksheet, s2 As Worksheet, s3 As Worksheet
    Set s1 = Sheets("Temp1")
    Set s2 = Sheets("Dashboard")
    Set s3 = Sheets("New_Sample")
lr = s2.Range("K" & Rows.Count).End(xlUp).row
lr2 = s3.Range("A" & Rows.Count).End(xlUp).row
lrtemp1 = s1.Range("A" & Rows.Count).End(xlUp).row
visiblerowcount = s1.UsedRange.SpecialCells(xlCellTypeVisible).Rows.Count

For i = 2 To lr

If AutoFilterMode = True Or FilterMode = True Then AutoFilterMode = False

On Error Resume Next

'If s2.Range("K" & i).Value = "N/A" Then

s1.Range("$A$1:$EC$" & lrtemp1).AutoFilter Field:=128, Criteria1:=s2.Range("K" & i).Value
            Set rng = s1.Range("$A$1:$EC$" & visiblerowcount).Offset(1, 0).SpecialCells(xlCellTypeVisible)

rng.Copy

s3.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
'End If
Next i


End Sub
 

Attachments

  • sample.JPG
    sample.JPG
    105.2 KB · Views: 14

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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