VBA字段包含

时间:2016-10-27 20:19:52

标签: vba pivot-table

我正在使用VBA在数据透视字段上执行搜索,我希望能够根据字段是否包含字符串的一部分进行搜索,但我不确定如何在不检查整个字符串的情况下执行此操作值。贝娄是我现在拥有的:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'This line stops the worksheet updating on every change, it only updates when cell
'P4 is touched
If Intersect(Target, Range("B4")) Is Nothing Then Exit Sub

'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewPull As String

'Here you amend to suit your data
Set pt = Worksheets("Pull Code Search").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Pull Code")
If IsEmpty(Range("B3").Value) = True Then
   NewPull = "(All)"
Else
   NewPull = Worksheets("Pull Code Search").Range("B3").Value
End If

'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewPull

    If NewPull = "(All)" Then
       ActiveSheet.PivotTables(1).PivotFields(1).ShowDetail = False
    End If

pt.RefreshTable
End With

End Sub

1 个答案:

答案 0 :(得分:1)

使用INSTR。

此函数返回字符串中第一次出现子字符串的位置。你不需要遍历整个字符串。

如果字符串(子字符串)的部分存在于实际的“字符串”中,则此函数返回正值。

INSTR功能只能用于Microsoft Excel中的VBA代码。

语法

InStr( [start], string, substring, [compare] )

此处有更多说明:

https://www.techonthenet.com/excel/formulas/instr.php