在多个工作表中搜索关键字,然后创建一个枢轴

时间:2019-06-25 15:33:11

标签: vba search pivot worksheet

我有一个代码来创建所需的数据透视表,但是我想添加代码以在每个标签中搜索关键字,如果找到,则突出显示该标签并创建数据透视表。在第一个选项卡中开始搜索Keyword =“ XXXXX”,如果找到并突出显示并在下面创建数据透视,则继续到下一个选项卡,循环直到完成。如果找不到关键字,则什么也不做,继续下一个标签。

Sub Create pivot()
ActiveSheet.Select
With ActiveWorkbook.ActiveSheet.Tab
    .Color = 65535
    .TintAndShade = 0
End With
Dim shtSrc As Worksheet, shtDest As Worksheet
Dim pc As PivotCache

Set shtSrc = ActiveSheet
Set shtDest = shtSrc.Parent.Sheets.Add()
shtDest.Name = shtSrc.Name & "-Pivot"

Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
    SourceData:=shtSrc.Range("A1").CurrentRegion)
pc.CreatePivotTable TableDestination:=shtDest.Range("A3"), _
    TableName:="PivotTable1"
With shtDest.PivotTables("PivotTable1")
    .ColumnGrand = True
    .HasAutoFormat = True
    .DisplayErrorString = False
    .DisplayNullString = True
    .EnableDrilldown = True
    .ErrorString = ""
    .MergeLabels = False
    .NullString = ""
    .PageFieldOrder = 2
    .PageFieldWrapCount = 0
    .PreserveFormatting = True
    .RowGrand = True
    .SaveData = True
    .PrintTitles = False
    .RepeatItemsOnEachPrintedPage = True
    .TotalsAnnotation = False
    .CompactRowIndent = 1
    .InGridDropZones = False
    .DisplayFieldCaptions = True
    .DisplayMemberPropertyTooltips = False
    .DisplayContextTooltips = True
    .ShowDrillIndicators = True
    .PrintDrillIndicators = False
    .AllowMultipleFilters = False
    .SortUsingCustomLists = True
    .FieldListSortAscending = False
    .ShowValuesRow = False
    .CalculatedMembersInFilters = False
    .RowAxisLayout xlCompactRow
    End With
    With ActiveSheet.PivotTables("PivotTable1").PivotCache
    .RefreshOnFileOpen = False
    .MissingItemsLimit = xlMissingItemsDefault
    End With
   ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
   ActiveWorkbook.ShowPivotTableFieldList = True
   With ActiveSheet.PivotTables("PivotTable1").PivotFields("Suspense?")
    .Orientation = xlRowField
    .Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField 
ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("APC LC2 Amount"), "Sum of APC LC2 
Amount", xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField 
ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("Partner LC2 Amount"), "Sum of Partner LC2 
Amount", _
    xlSum
ActiveSheet.PivotTables("PivotTable1").AddDataField 
ActiveSheet.PivotTables( _
    "PivotTable1").PivotFields("LC2 Amount"), _
    "Sum of LC2 amount", xlSum

 End Sub

0 个答案:

没有答案