在Excel中旋转而不聚合,显示文本,而不是数字?

比方说,我有一个这样的表格:

Country Region Mytext USA North a USA South b Brasil North c Brasil South d 

我如何在Excel中获得这样的数据透视表?

 Country North South USA ab Brasil cd 

如果'mytext'是一个数字,我可以做一个数据透视表:因为每个国家和地区的组合只有一行,min = max = sum = avg,实际上这些聚合函数中的任何一个都会显示结果我想要。

但是,当我想要的字段是非数字的时候呢? 对于Python的pandas库,我甚至可以在非数字字段上使用dataframe.pivot()方法,但是我还没有find在Excel中执行相同的方法。 我可以将一个数字与每一行相关联,在Excel中进行数据透视,以显示该数字,然后执行查找以获取关联的文本,但对于某些应该更容易的事情来说,这似乎是一个相当复杂的过程!

您可以使用MS Power Query加载项 *。 旋转表是不容易的。 它是免费的,非常有效的数据转换。

M代码

  let Source = Excel.CurrentWorkbook(){[Name="table1"]}[Content], #"Pivot column" = Table.Pivot(Source, List.Distinct(Source[Region]), "Region", "Mytext") in #"Pivot column" 

你的输出 在这里输入图像描述

*从MS Office 2016,它完全集成在Excel中作为获取和转换function。

将MyText添加为国家/地区标签下面的行标签,然后以“数据透视表”devise选项卡的“表格”格式显示数据透视表。 为国家/地区行标签字段重复项目标签。

这可以完成,但要求VBA用一些文本临时覆盖数据透视表中的数字“占位符”值。 要启用此function,您需要为数据透视表设置EnableDataValueEditing = True。 刷新时,您将对数字占位符值进行查找,并将其replace为文本。 懊恼,是的。 解决方法,是的。 但它是诀窍。 请注意,这只是“坚持”,直到下一次数据透视表刷新,所以这个代码需要在每次刷新触发。

这是我在自己的项目中使用的代码。 把它放在一个标准代码模块中:

 Function Pivots_TextInValuesArea(pt As PivotTable, rngNumeric As Range, rngText As Range, Optional varNoMatch As Variant) Dim cell As Range Dim varNumeric As Variant Dim varText As Variant Dim varResult As Variant Dim bScreenUpdating As Boolean Dim bEnableEvents As Boolean Dim lngCalculation As Long On Error GoTo errHandler With Application bScreenUpdating = .ScreenUpdating bEnableEvents = .EnableEvents lngCalculation = .Calculation .ScreenUpdating = False .EnableEvents = False .Calculation = xlCalculationManual End With varNumeric = rngNumeric varText = rngText pt.EnableDataValueEditing = True 'Setting this to TRUE allow users or this code to temporarily overwrite PivotTable cells in the VALUES area. 'Note that this only 'sticks' until the next time the PivotTable is refreshed, so this code needs to be ' triggerred on every refresh For Each cell In pt.DataBodyRange.Cells With cell varResult = Application.Index(varText, Application.Match(.Value2, varNumeric, 0)) If Not IsError(varResult) Then cell.Value2 = varResult Else: If Not IsMissing(varNoMatch) Then cell.Value2 = varNoMatch End If End With Next cell errHandler: If Err.Number > 0 Then If gbDebug Then Stop: Resume Else: MsgBox "Whoops, there was an error: Error#" & Err.Number & vbCrLf & Err.Description _ , vbCritical, "Error", Err.HelpFile, Err.HelpContext End If End If With Application .ScreenUpdating = bScreenUpdating .EnableEvents = bEnableEvents .Calculation = lngCalculation End With End Function 

结果如下:VALUES区域中带有文本的数据透视表:

在这里输入图像说明

这里是“转换”表,告诉代码显示每个数字:

在这里输入图像说明

我已经为这些分配了两个命名范围,以便我的代码知道在哪里查找查找表:

  • tbl_PivotValues.TextValue
  • tbl_PivotValues.NumericValue

…以下是我如何触发函数并传递所需的参数:

 Option Explicit Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable) If Target.Name = "GroupView" Then Pivots_TextInValuesArea Target, [tbl_PivotValues.NumericValue], [tbl_PivotValues.TextValue] End Sub 

该代码将进入工作表代码模块对应于数据透视表是工作表:

在这里输入图像描述

对于未来的读者 – 一起使用两个function:

function区>数据透视表工具>报表布局>以表格forms显示

function区>数据透视表工具>报表布局>重复所有项目标签

行字段将作为垂直重复的文本从左到右呈现。