在Excel VBA中使用通配符

时间:2019-05-01 11:16:06

标签: excel vba wildcard

我正在尝试在公式中使用通配符来对表列中包含文本而不是""的单元格进行计数

我尝试了以下方法:

  • 字符串比较
Dim g As Integer

g = Application.WorksheetFunction.CountIf(ThisWorkbook.Worksheets("ws1").ListObjects("Table1").ListColumns("ColumnA").DataBodyRange, ""*?"")
  • 使用波浪号失败:
Dim g As Integer

g = Application.WorksheetFunction.CountIf(ThisWorkbook.Worksheets("ws1").ListObjects("Table1").ListColumns("ColumnA").DataBodyRange, ""~*?"")
  • 使用下面的ASCII字符返回0:
g = Application.WorksheetFunction.CountIf(ThisWorkbook.Worksheets("ws1").ListObjects("Table1").ListColumns("Column1").DataBodyRange, Chr(34) & Chr(63) & Chr(42) & Chr(34))

1 个答案:

答案 0 :(得分:1)

经过测试:

Public Function not_qt(ByVal rng As Range) As Integer

    Dim cell As Range
    Dim counter As Integer: counter = 0

    For Each cell In rng
        If Not IsEmpty(cell) Then
            If Not cell Like Chr(34) & "*" & Chr(34) Then
                counter = counter + 1
                'cell.Offset(0, 1) = counter   '<- Only for illustration purposes
            End If
        End If
    Next cell

    not_qt = counter

End Function

enter image description here