首页 > 解决方案 > 对过滤的单元格使用工作表函数 countif

问题描述

我正在使用下面的代码循环通过过滤列,并在循环时收集销售人员的每个姓名;我正在使用 SpecialCells(xlCellTypeVisible),它只拾取可见的行。我只想要名称一次,所以我使用 countif 工作表函数。获得名称后,我会将其存储在临时变量 (namelist_tmp) 中并添加分号。最终的游戏是创建一个变量(名称列表),每个名称用分号分隔,这样我就可以在我拥有的一些电子邮件代码的 .to 部分中使用它。

我能够遍历并捕获名称,但是当我尝试将临时变量分配给最终变量时,我在 namelist_tmp 代码处收到运行时错误 5 Invalid Procedure call or argument: namelist = Left(namelist_tmp, Len(namelist_tmp) - 2)

我不确定我是否以正确的方式前进,善良的灵魂可以帮助我看到我的方式的错误吗?

Sub filteredstuff()
Dim lastRow As Long, myrange As Range
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
For Each myrange In Range("E11:E" & lastRow).SpecialCells(xlCellTypeVisible)
'At each name, use COUNTIF to look upwards and see if the name already exists.
'If not, add name, semi-colon and space to temporary name list
         If WorksheetFunction.CountIf(Range("E11:E" & myrange.Row), _
         Range("E" & myrange.Row)) < 2 Then
             namelist_tmp = namelist_tmp & Range("E" & myrange.Row).Value & "; "
         End If
'Strip last semi-colon & space off of temp list
             namelist = Left(namelist_tmp, Len(namelist_tmp) - 2)
Next myrange
MsgBox namelist
End Sub

标签: excelvba

解决方案


我已经在虚拟数据上运行了您的代码,对我来说它有效,我没有收到任何错误。而且这条线很完美,应该没问题:

namelist = Left(namelist_tmp, Len(namelist_tmp) - 2)

你能提供更多细节吗?


推荐阅读