VBA: Filter issue

sunilbsrv2k

Board Regular
Joined
May 25, 2018
Messages
73
Hi All,

I have written a VBA macro that will filter a table based on the values (Employee ID) in another sheet.

This will further extract randomly one row for each filtered criteria.

It is running fine; however each time, the code executes successfully but omits 3 to 4 values (Employee IDs) though the data exists for them in the table.

Could you please help. Below is my code:

VBA Code:
Sub Multi_Task()
Dim wb, wb1 As Workbook
Dim ws, ws1 As Worksheet
Dim rng, rng1 As Range
Dim i, n As Integer
'Sheets("Sheet2").Range("A:AH").Clear
ThisWorkbook.Sheets("Sheet3").Range("A:AH").Clear
ThisWorkbook.Sheets("Sheet1").Activate
Set rng = ThisWorkbook.Sheets("Sheet1").Range("D2:D60")
Set wb = Workbooks.Open("File name")
Set ws = wb.Worksheets("MEMBERLOOKUP TASK")
ws.Range("$A$1:$L" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).AutoFilter Field:=7

forloop:
For Each cell In rng
ThisWorkbook.Sheets("Sheet2").Range("A:AH").Clear
'ThisWorkbook.Sheets("Sheet1").Activate
'If cell.Value = "" Then GoTo forloop
MsgBox cell.Row & "," & cell
ws.Activate
ws.Range("A1").AutoFilter Field:=7, Criteria1:=cell.Value
i = Application.WorksheetFunction.Subtotal(3, Range("A1:A65536")) ' & Rows.Count).End(xlUp).Offset(1, 0))
'MsgBox i ' Range("A22:A" & Rows.Count).End(xlUp).Offset(1, 0)

Set arng = Range("A22:L" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible) '.Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy
arng.Copy Destination:=ThisWorkbook.Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
'ThisWorkbook.Worksheets("Sheet2").Range("A2") = arng
'Next
'MsgBox n & " ," & n

'Sheets("Sheet2").Activate
'Sheets("Sheet2").Range("A:AH").Clear
Rndm = RndInt(3, i)
For n = 2 To i - 1
'Sheets("Sheet2").Range("A:AH").Clear
If n = Rndm Then
'MsgBox n & "," & Rndm
'ThisWorkbook.Sheets("Sheet2").Range("A" & n).EntireRow.Copy Destination:=ThisWorkbook.Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
ThisWorkbook.Sheets("Sheet2").Range("A" & n).EntireRow.Copy Destination:=wb.Sheets("MEMBERLOOKUP TASK SAMPLING").Range("B" & Rows.Count).End(xlUp).Offset(1, -1)
'ThisWorkbook.Sheets("Sheet2").Range(Cells("A" & n), Cells("L" & n)).Copy 'Destination:=wb.Sheets("FMM TASK SAMPLING").Cells("B" & Rows.Count).End(xlUp).Offset(1, 0)
'wb.Sheets("FMM TASK SAMPLING").Cells("B" & Rows.Count).End(xlUp).Offset(1, 0).Paste

End If

Next

Next
ws.Range("$A$1:$L" & Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).AutoFilter Field:=7
wb.Save
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,

Please let me know how I can share the data with you, as I my not be able to share it publicly.

Thanks,
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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