excel 使用单元格列表数据验证对单元格中以逗号分隔的单个值进行计数

9rygscc1  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(142)

在我的工作表的E列,我有一个单元格中的下拉列表数据验证选项(CAIOS,ERCOT,MISO,PJM,SPP)。我写了一个扩展名来允许每个单元格中的多个选择。我有单元格[PJM],[PJM],[PJM],[PJM,SPP]。我试图计算单元格中是否包含PJM。我使用通配符,但结果总是4而不是5。

cpjpxq1n

cpjpxq1n1#

试试这个案例:

Function CountPJMOccurrences(cellValue As String) As Long
    Dim selections() As String
    Dim i As Long
    Dim countPJM As Long
    
    ' Split cell contents based on comma
    selections = Split(cellValue, ",")
    
    ' Loop through the array and count occurrences of "PJM"
    For i = LBound(selections) To UBound(selections)
        If Trim(selections(i)) = "PJM" Then
            countPJM = countPJM + 1
        End If
    Next i
    
    CountPJMOccurrences = countPJM
End Function

字符串
您可以在工作表中使用此函数;例如,如果包含“排序列表”的单元格位于单元格E1中,则可以在F1中输入以下公式:

=CountPJMOccurrences(E1)

e1xvtsh3

e1xvtsh32#

我使用GPT来构建Mark的答案,最后使用了这个,它起作用了。

Function CountAESOOccurrencesInColumn() As Long
    Dim ws As Worksheet
    Dim cell As Range
    Dim countAESO As Long
    
    ' Set the worksheet
    Set ws = ThisWorkbook.Sheets("RFP_Details")
    
    ' Initialize count
    countAESO = 0
    
    ' Loop through all cells in column E
    For Each cell In ws.Range("E:E")
        If InStr(1, cell.Value, "Canada-AESO", vbTextCompare) > 0 Then
            countAESO = countAESO + 1
        End If
    Next cell
    
    ' Return the count
    CountAESOOccurrencesInColumn = countAESO
End Function

字符串

相关问题