hellodj143
New Member
- Joined
- Sep 3, 2022
- Messages
- 2
- Office Version
- 365
- Platform
- 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.
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