Let's modify your function a little bit and have it return a value:
Function TokenNotActivatedProcess() As Boolean
Dim r As Range, n As Long, itm, FirstRow As Long, ret as Boolean
n = Cells(Rows.Count, 1).End(xlUp).Row
Set r = Range("A1:A" & n).Cells.SpecialCells(xlCellTypeVisible)
FirstRow = ActiveSheet.Range("F2").End(xlDown).Row
itm = ActiveSheet.Range("F" & FirstRow).Value
If r.Count - 2 > 0 Then
Debug.Print itm & " - " & r.Count - 2
ret = True
End If
TokenNotActivatedProcess = ret
End Function
Then, you can change your For each itm in Col
loop. Instead of calling the function, just evaluate it as part of boolean logic, since it returns a boolean, you can do this.
Dim ws As Worksheet
Set ws = ActiveSheet
For Each itm In Col
ws.Range("A2:Z2").AutoFilter Field:=6, Criteria1:=itm
If TokenNotActivatedProcess Then
'Dim wbNew as Workbook
'Set wbNew = Workbooks.Add
'
'### Add code here which will create a new workbook
' and copy the data to the new workbook.
' This would probably be another subroutine or function.
'
'wbNew.SaveAs "C:\new file.xlsx"
'wbNew.Close
End If
Next
This will fix it eventually, but you are relying on Activate
and Selection
methods, which becomes very problematic when you are working with multiple workbooks, as discussed here:
How to avoid using Select in Excel VBA macros
I modified the loop above to avoid this but there may be other places you need to fix.
If you have trouble modifying your code to avoid using Activate/Select methods, or if you have problems adding new workbooks to copy the data, just update your question with your current code. It should not be very difficult to do this.