所以,我有一个"狭窄"我正试图变成一个" Wide"表。好吧,离开VBA我们会在合理的时间内完成这项工作。让我们首先移动我们的keyID,我们只需要移动唯一值。代码:
Sub Flatten()
Dim RawTable As ListObject
Dim FlatTable As ListObject
Dim SWS As Worksheet ' Source worksheet
Dim DWS As Worksheet ' Destination worksheet
Dim wb As Workbook
Dim HeaderCheck As Boolean
Dim FlatLastRow As Long
Dim MoveRange As Range
Dim LastColFlat As Long
Set wb = ThisWorkbook
Set SWS = wb.Sheets("RawData")
Set DWS = wb.Sheets("Flattened Data")
HeaderCheck = wb.Names("HeaderIntegrityCheck").RefersToRange.Value2
If HeaderCheck Then
'Everything's good
Else
'Problem
MsgBox "Bad data entered into the RawData tab, headers aren't as expected. Please fix. The macro will now end"
End
End If
Set RawTable = SWS.ListObjects("RawTable")
Set FlatTable = DWS.ListObjects("FlatTable")
Set MoveRange = RawTable.ListColumns(3).DataBodyRange
LastColFlat = DWS.Cells(1, Columns.Count).End(xlToLeft).Column
FlatTable.Resize FlatTable.Range.Resize(CountUnique(MoveRange) + 1, LastColFlat)
MoveRange.AdvancedFilter Action:=xlFilterCopy, copytorange:=FlatTable.ListColumns(2).DataBodyRange, unique:=True
End Sub
问题:前两个ID会重复/不唯一。例如,2155904666,2155904666,2155904659,2155904659;将移动2155904666,2155904666,2155904659。如果我反转它,我得到2155904659,2155904659,2155904666。这发生在具有大型数据集的微小数据集上 - 前两个总是在移动。这也会导致错误触发,因为它正确计算了有多少个唯一ID,然后尝试再将一行添加到表中。
Hack修复:调整大小为+2,测试第一条数据线是否与第二条数据线相同,如果它们是重复的,则删除其中一条线,然后调整大小。这是有效的,但它笨拙而且不优雅,并且没有解决为什么移动这些值的根本原因,因为唯一的移动它们并不是唯一的。
PS:如果有人有更好的独特移动方法,那么我的所有耳朵都是如此。使脚本运行所需的Microsoft脚本库。