使用 Excel 中的节点级别转换 table
Transform a table with node levels in Excel
如何实现数据从 Table 1 到 Table 2 的转换。
“=offset()”公式对这种情况有帮助吗?
所以这是我为解决问题而编写的代码:
Sub MacroTest()
Dim iVal As Integer
Dim SearchValue As String
Dim WholeRange As String
Dim RangePart As String
Dim EndColumnToCut As Integer
Dim PasteCell As String
Application.ScreenUpdating = False 'Turn off window update while running macro
X = 3 'Row to start from
Y = 12 'Column Value where the table ends
Do Until IsEmpty(Cells(X, Y)) 'Start at Row X and loop through column Y
If IsError(ActiveSheet.Cells(X, Y)) Then 'Check if cell(X,Y) returns error code, if yes then skip current code
GoTo NextRow
Else
SearchValue = ActiveSheet.Cells(X, Y).Value 'Find the value in cell (X,Y)
iVal = Application.WorksheetFunction.CountIf(Range("B" & X, "L" & X), SearchValue) 'Count how many times the value in "SearchValue" appears (You want to remove duplicates and leave one unique value)
Cells_To_Remove = iVal - 1 'delete all cells with the same value except one
If Cells_To_Remove = 0 Then 'If "Cells_To_Remove" is equal to 0, i.e. there are no cells to remove, then go to next row
GoTo NextRow 'Skip cut and paste values and jump to next row
Else
RemoveValuesInRange = Range(Cells(X, Y - Cells_To_Remove + 1), Cells(X, Y)).ClearContents 'Clear Contents in the range
RemoveFormatInRange = Range(Cells(X, Y - Cells_To_Remove + 1), Cells(X, Y)).ClearFormats 'Clear formats in range
EndColumnToCut = Y - Cells_To_Remove 'Define the last column in range to cut data
RangePart = Range(Cells(X, 2), Cells(X, EndColumnToCut)).Select 'Define the whole range where the data should be cut
Selection.Cut 'Cut data
PasteCell = Cells(X, 2 + Cells_To_Remove).Select 'Define the cell where to paste the data
ActiveSheet.Paste 'Paste Data
End If
End If
GoTo NextRow 'Jump to next row
NextRow:
X = X + 1 'Jump to next row
Loop
End Sub
这个问题一直萦绕在我的脑海中。忍不住解决了。
首先,计算一行中的唯一值(节点 + 父级)。在 N3 单元格中输入公式并向下填充:
=SUMPRODUCT(1/COUNTIF(B3:L3,B3:L3&""))
有关公式工作原理的详细信息:https://exceljet.net/formula/count-unique-values-in-a-range-with-countif
接下来,转到 L15 并为每个级别插入编号:1 作为父级,2 - 节点 1,...,11 - 节点 10。
在L17输入公式复制到整个范围B17:J25
=IF(L<=$N3,OFFSET($B3,0,$N3-L);"")
公式的工作原理如下。它锚定在每一行的节点 10 值。根据行中唯一值的数量,它从左到右提取值。例如,如果您在 Apple 行(包括 Parent)中有 4 个唯一值,则该公式将仅从列 B、C、D、E 中提取值。
这个过程我可能无话可说,看看table:
https://www.dropbox.com/s/n1y53v3nye3n47p/Transform%20Table.xlsx?dl=0
如何实现数据从 Table 1 到 Table 2 的转换。
“=offset()”公式对这种情况有帮助吗?
所以这是我为解决问题而编写的代码:
Sub MacroTest()
Dim iVal As Integer
Dim SearchValue As String
Dim WholeRange As String
Dim RangePart As String
Dim EndColumnToCut As Integer
Dim PasteCell As String
Application.ScreenUpdating = False 'Turn off window update while running macro
X = 3 'Row to start from
Y = 12 'Column Value where the table ends
Do Until IsEmpty(Cells(X, Y)) 'Start at Row X and loop through column Y
If IsError(ActiveSheet.Cells(X, Y)) Then 'Check if cell(X,Y) returns error code, if yes then skip current code
GoTo NextRow
Else
SearchValue = ActiveSheet.Cells(X, Y).Value 'Find the value in cell (X,Y)
iVal = Application.WorksheetFunction.CountIf(Range("B" & X, "L" & X), SearchValue) 'Count how many times the value in "SearchValue" appears (You want to remove duplicates and leave one unique value)
Cells_To_Remove = iVal - 1 'delete all cells with the same value except one
If Cells_To_Remove = 0 Then 'If "Cells_To_Remove" is equal to 0, i.e. there are no cells to remove, then go to next row
GoTo NextRow 'Skip cut and paste values and jump to next row
Else
RemoveValuesInRange = Range(Cells(X, Y - Cells_To_Remove + 1), Cells(X, Y)).ClearContents 'Clear Contents in the range
RemoveFormatInRange = Range(Cells(X, Y - Cells_To_Remove + 1), Cells(X, Y)).ClearFormats 'Clear formats in range
EndColumnToCut = Y - Cells_To_Remove 'Define the last column in range to cut data
RangePart = Range(Cells(X, 2), Cells(X, EndColumnToCut)).Select 'Define the whole range where the data should be cut
Selection.Cut 'Cut data
PasteCell = Cells(X, 2 + Cells_To_Remove).Select 'Define the cell where to paste the data
ActiveSheet.Paste 'Paste Data
End If
End If
GoTo NextRow 'Jump to next row
NextRow:
X = X + 1 'Jump to next row
Loop
End Sub
这个问题一直萦绕在我的脑海中。忍不住解决了。
首先,计算一行中的唯一值(节点 + 父级)。在 N3 单元格中输入公式并向下填充:
=SUMPRODUCT(1/COUNTIF(B3:L3,B3:L3&""))
有关公式工作原理的详细信息:https://exceljet.net/formula/count-unique-values-in-a-range-with-countif
接下来,转到 L15 并为每个级别插入编号:1 作为父级,2 - 节点 1,...,11 - 节点 10。
在L17输入公式复制到整个范围B17:J25
=IF(L<=$N3,OFFSET($B3,0,$N3-L);"")
公式的工作原理如下。它锚定在每一行的节点 10 值。根据行中唯一值的数量,它从左到右提取值。例如,如果您在 Apple 行(包括 Parent)中有 4 个唯一值,则该公式将仅从列 B、C、D、E 中提取值。
这个过程我可能无话可说,看看table: https://www.dropbox.com/s/n1y53v3nye3n47p/Transform%20Table.xlsx?dl=0