我的代码有点麻烦。我试图过滤M列中的值,然后将M中的一个值设置为变量deptName。这适用于除一个之外的每个迭代,而不是将deptName设置为M中的值,而是将其设置为等于A1中的值。它只是为迭代中的一个做到这一点,我不知道为什么。Excel vba过滤数据并将过滤列表中的值设置为变量
For criteria = 1 To UBound(degreeArray)
degreeWS.range(fields).AutoFilter Field:=degreeColumn, Criteria1:=degreeArray(criteria)
degreeWS.range("A2:A" & lrd).EntireRow.Copy
Dim deptName As Variant
range("M2", Cells(Rows.count, "M").End(xlUp)).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
deptName = Selection
下面是数据的一个例子
A B C D E F G H I J K L M
ID FName LName Degree Major Col Dept1 Dept1Name Major2 Col Dept2 Dept2Name
100 Jack Smith MBA MAJOR1 UK BIO BIOLOGY MAJOR2 UK CHEM CHEMISTRY
101 Sally Johnson BS MAJOR1 UK EDU EDUCATION MAJOR2 UK BIO BIOLOGY
102 Bryan Carter BSB MAJOR1 UK CHEM CHEMISTRY MAJOR2 UK EDU EDUCATION
104 Mason Harper BS MAJOR1 UK BIO BIOLOGY MAJOR2 UK EDU EDUCATION
104 Harry Potter MBA MAJOR1 UK CHEM CHEMISTRY MAJOR2 UK BIO BIOLOGY
避免使用选择不惜一切代价.... – Lowpar
什么可能我改用选择@Lowpar – Cocoberry2526
的@ Cocoberry2526使用类似:'DEPTNAME =范围( “M2”,细胞(Rows.count,“M “).End(xlUp))。SpecialCells(xlCellTypeVisible).Cells(1,1).value' – scb998