根据单元格中的数字多次重复上一行的公式

时间:2019-06-15 19:20:25

标签: excel vba excel-formula unique repeat

我有多行数据,每行涉及x个雌性和x个雄性。

我需要将它们分解成单独的记录(每个记录1行)。是否有一个excel公式可以查看第一行,并根据D列中的数字返回该行数,并且a,b,c中的数据相同,但是,对于e,f,g,h列,用另一张纸上的1全部替换那么多记录?

查看屏幕图像。有9行1级为1,有17行2级为1,依此类推。我尝试了一些基本公式组合,这些组合虽然有效但很耗时,并且有1342行需要分解为单独的记录。 enter image description here

这是您建议戴维(David)取消枢纽之后的工作表。 Table after unpivot

codeandoutput

5个级别的代码,带有慢速问题的代码 子CreateIndividualRecordsLevel5()     昏昏欲睡     昏昏欲睡     昏暗b只要     点心只要     暗淡如久     尽可能长     尽可能长     Dim instanceInRow只要长     昏暗等级1     昏暗等级2     昏暗等级3     昏暗等级4     昏暗等级5     昏暗的wb作为工作簿     作为工作表昏暗

{{1}}

1 个答案:

答案 0 :(得分:0)

对不起,我犯了一个错误,花了更长的时间。我们开始:

在此处检查图像与结果>>> enter image description here

    Private Sub testcopyrow()
    Dim i As Long
    Dim a As Long
    Dim b As Long
    Dim c As Long
    Dim d As Long
    Dim e As Long
    Dim n As Long
    Dim instancesInRow As Long
    Dim level1 As Long
    Dim level2 As Long
    Dim level3 As Long
    Dim level4 As Long
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("SplitRowsByInstances")

    n = 0

    ' Outer loop 'goes' through your original table and for each cell in
    ' column D checks how many instances there are.
    For i = 2 To 7

            instancesInRow = ws.Cells(i, 4).Value
            level1 = ws.Cells(i, 5).Value
            level2 = ws.Cells(i, 6).Value
            level3 = ws.Cells(i, 7).Value
            level4 = ws.Cells(i, 8).Value

            ' Inner loop 'knows' how many times it needs to loop-copy the same
            ' row into the new table/worksheet.
            For a = 1 To level1
                    n = n + 1
                    ws.range("J" & n & ":M" & n).Value = ws.range("A" & i & ":H" & i).Value
                    ws.range("N" & n).Value = 1
                    ws.range("O" & n).Value = 0
                    ws.range("P" & n).Value = 0
                    ws.range("Q" & n).Value = 0
            Next a

            For b = 1 To level2
                    n = n + 1
                    ws.range("J" & n & ":M" & n).Value = ws.range("A" & i & ":H" & i).Value
                    ws.range("N" & n).Value = 0
                    ws.range("O" & n).Value = 1
                    ws.range("P" & n).Value = 0
                    ws.range("Q" & n).Value = 0
            Next b

            For c = 1 To level3
                    n = n + 1
                    ws.range("J" & n & ":M" & n).Value = ws.range("A" & i & ":H" & i).Value
                    ws.range("N" & n).Value = 0
                    ws.range("O" & n).Value = 0
                    ws.range("P" & n).Value = 1
                    ws.range("Q" & n).Value = 0
            Next c

            For d = 1 To level4
                    n = n + 1
                    ws.range("J" & n & ":M" & n).Value = ws.range("A" & i & ":H" & i).Value
                    ws.range("N" & n).Value = 0
                    ws.range("O" & n).Value = 0
                    ws.range("P" & n).Value = 0
                    ws.range("Q" & n).Value = 1
            Next d


                    ' Counter n counts how many rows you have copied so far,
                    'in order to know in which row to copy to the new range

    Next i

    End Sub

让我知道我是否帮忙!