答案1
選擇對你沒有任何幫助。選擇和 ActiveSheet 使用並不安全。
您必須定義變數
Dim sheet1 As Worksheet, sheet2 As Worksheet
Dim range1 As Range, range2 As Range
然後取得並使用工作表和範圍的參考。喜歡:
Set sheet1 = ThisWorkbook.Sheets("Sheet1")
Set range1 = ThisWorkbook.Sheets("Sheet2").Range("T8")
sheet1.Range("$A:$CT").AutoFilter Field:=16, Criteria1:=range1.Value
或者
Set range1 = ThisWorkbook.Sheets("Sheet1").Range("$A:$CT")
Set range2 = ThisWorkbook.Sheets("Sheet2").Range("T8")
range1.AutoFilter Field:=16, Criteria1:=range2.Value
或者
Set sheet1 = ThisWorkbook.Sheets("Sheet1")
Set sheet2 = ThisWorkbook.Sheets("Sheet2")
sheet1.Range("$A:$CT").AutoFilter Field:=16, Criteria1:=sheet2.Range("T8").Value
等等...