Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 创建宏以生成透视表+;直方图不起作用_Excel_Vba_Histogram - Fatal编程技术网

Excel 创建宏以生成透视表+;直方图不起作用

Excel 创建宏以生成透视表+;直方图不起作用,excel,vba,histogram,Excel,Vba,Histogram,我使用grep制作了一个日志文件解析工具,它创建了两个文本文件。1个文本文件包含所有错误代码+错误消息,另一个仅包含错误代码 我通过一个简单的vba脚本将它们导入excel文件。然后我有两个宏,可以将文本转换为列,这样excel就可以更容易地读取它们,这很好 错误代码+错误消息(见第2页)仅包含宏后的代码+消息,以将其放入列中,然后删除双精度,使其成为一种列表,用户可以在其中查看哪些错误代码给出了哪些消息。这没关系 然后将错误代码放入表1中,宏只创建a列中所有代码的列表 现在,我想做的是有一个宏

我使用grep制作了一个日志文件解析工具,它创建了两个文本文件。1个文本文件包含所有错误代码+错误消息,另一个仅包含错误代码

我通过一个简单的vba脚本将它们导入excel文件。然后我有两个宏,可以将文本转换为列,这样excel就可以更容易地读取它们,这很好

错误代码+错误消息(见第2页)仅包含宏后的代码+消息,以将其放入列中,然后删除双精度,使其成为一种列表,用户可以在其中查看哪些错误代码给出了哪些消息。这没关系

然后将错误代码放入表1中,宏只创建a列中所有代码的列表

现在,我想做的是有一个宏,它将获取所有这些错误代码(范围将根据解析的日志文件以及它将包含多少错误代码而有所不同),并将它们放入直方图中,以便用户可以直观地看到哪些错误代码最多

我的理想情况是,用户只需定义一条路径,然后所有其他操作都会自动进行,直到直方图出现,并清楚地显示出哪个错误最多

到目前为止,我得到的是grep解析日志文件并生成一些新的txt文件。然后,Excel文件将自动打开,txt文件将自动放入正确的工作表中。然后宏正在执行它们的操作

我需要帮助的是如何创建直方图的自动化。我试着在谷歌和这里搜索了很多东西,但是大部分的代码都是如此复杂和庞大,这对我在这里要做的事情毫无帮助。我希望我能找到一个简单的方法来解决这个问题

提前感谢您的想法

录制Norie建议的宏时,出现以下错误:

运行时错误“1004”应用程序定义或对象定义错误

这是VBA随后自动生成的代码:

Option Explicit
Sub Histogram()
'
' Histogram Macro
'

'
    Columns("A:A").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R107C1", Version:=6).CreatePivotTable TableDestination:= _
        "Sheet10!R3C1", TableName:="PivotTable25", DefaultVersion:=6
    Sheets("Sheet10").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable25")
        .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("PivotTable25").PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    ActiveSheet.PivotTables("PivotTable25").RepeatAllLabels xlRepeatLabels
    With ActiveSheet.PivotTables("PivotTable25").PivotFields("40520")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable25").AddDataField ActiveSheet.PivotTables( _
        "PivotTable25").PivotFields("40520"), "Count of 40520", xlCount
    ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
    ActiveChart.SetSourceData Source:=Range( _
        "[From notepad to excel test 1.xlsm]Sheet10!PivotTable25")
End Sub

似乎来源不正确。我尝试将
“Sheet1!R1C1:R107C1”
更改为
“Sheet1!$A:$A”
,但这也没有帮助。

我不会说源代码是错误的,问题是它是硬编码的

您需要对其进行更改,以使其不会硬编码,特别是您需要更改107以反映表1上A列中的最后一行数据

此外,还有一些东西被赋予了看似任意的名称,例如PivotTable25——如果您在代码中使用特定的名称会更好

另外,您应该在列中添加一个带有错误代码的标题

在下面的代码中,假设数据位于Sheet1上,标题为“Errors”,并且“ErrorPivot”已用于正在创建的数据透视表的名称

Sub Histogram()
Dim wsPivot As Worksheet
Dim chtErrors As Chart
Dim ptErrors As PivotTable
Dim lngLastRow As Long

    lngLastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    Set wsPivot = Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "Sheet1!R1C1:R" & lngLastRow & " C1", Version:=6).CreatePivotTable TableDestination:= _
        wsPivot.Name & "!R3C1", TableName:="ErrorPivot", DefaultVersion:=6
        
    Set ptErrors = wsPivot.PivotTables("ErrorPivot")
    
    With ptErrors
        .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 ptErrors.PivotCache
        .RefreshOnFileOpen = False
        .MissingItemsLimit = xlMissingItemsDefault
    End With
    
    ptErrors.RepeatAllLabels xlRepeatLabels
    
    ptErrors.AddDataField ptErrors.PivotFields("Errors"), "Count of Errors", xlCount
        
    With ptErrors.PivotFields("Errors")
        .Orientation = xlRowField
        .Position = 1
    End With
    
    Set chtErrors = wsPivot.Shapes.AddChart2(201, xlColumnClustered).Chart
    
    chtErrors.SetSourceData Source:=ptErrors.DataBodyRange
    
End Sub

您是否尝试过打开宏记录器并手动创建直方图和透视表?这将为您提供开始的代码。诚然,生成的代码会很混乱,但应该可以整理一下。它还将有各种硬编码的东西,例如范围,但同样可以固定。使用宏记录器后,确保读取并清理代码,使其工作可靠。我编辑了这篇文章以反映您的评论。在数据中,“40520”代表什么?这是一个错误代码吗?标题?40520实际上是来自日志文件的错误代码。它是列表中的第一个,并自动作为透视表的标题。我相信我可以在以后更改。非常感谢,这是完美的工作!