Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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_If Statement_Numbers_Offset - Fatal编程技术网

如何在excel中创建智能偏移?

如何在excel中创建智能偏移?,excel,if-statement,numbers,offset,Excel,If Statement,Numbers,Offset,我有两列-一列是项目编号,另一列是项目中包含的子编号。 我想做的是创建一个智能偏移-所以如果有一个项目编号,我不想看到子编号。我想在下面的行中看到子编号,等等 我现在的情况如下: =IFA14=A13,,A15 =IFE15、IFE14、B14、B15 然而,它并不能完美地工作——它总是在一行中丢失带有项目计划编号的里程碑子项目 我附上了这张照片,因为很难解释 如果有帮助的话,我可以添加额外的列 谢谢你的帮助, Marek您可以创建一个数据透视表来轻松完成此任务: 我开始使用VBA,唯一需要更改

我有两列-一列是项目编号,另一列是项目中包含的子编号。 我想做的是创建一个智能偏移-所以如果有一个项目编号,我不想看到子编号。我想在下面的行中看到子编号,等等

我现在的情况如下: =IFA14=A13,,A15 =IFE15、IFE14、B14、B15

然而,它并不能完美地工作——它总是在一行中丢失带有项目计划编号的里程碑子项目

我附上了这张照片,因为很难解释

如果有帮助的话,我可以添加额外的列

谢谢你的帮助,
Marek

您可以创建一个数据透视表来轻松完成此任务:


我开始使用VBA,唯一需要更改的是每次VBA插入一行时,每个I值都必须增加1。所以,如果VBA插入一行,它将是i+1和i+2,如果VBA插入两行,它将是i+2和更低的i+3,等等。。。有没有办法达到这一点

Sub test()

Worksheets(2).Activate

lastrow = Range("A3").End(xlDown).Row

For i = 3 To lastrow

    If Cells(i, 8) <> "" Then
    Range(Cells(i + 1, 8), Cells(i + 1, 9)).Select
    Selection.Insert Shift:=xlDown
    Cells(i + 1, 9).Value = Cells(i, 2).Value
    Else
    Cells(i + 1, 9).Value = Cells(i, 2).Value
    End If

Next i

End Sub

下面是一个VBA解决方案。当前需要将表格转换为正式的excel表格,可以通过高亮显示数据表格、转到“插入”功能区并选择表格来实现。这样做的好处是,即使要在工作表中移动表格,此代码也可以工作

我称我的专栏为initiative和milestone,这与代码第8行有关。这些列必须相邻才能使代码正常工作。你必须改变它以适应你的情况。您可能还需要更改第10行中startCell变量的值

Public Sub CreateInitiativeListForGnatt()
    Dim tbl As ListObject 'The table object in your worksheet.
    Dim projCols As Range 'The initiative and milestone columns in the table.
    Dim projDict As Object 'The dictionary object we'll use to store key-values of initiative-[milestone1, milestone2, etc]
    Dim startCell As Range

    Set tbl = Worksheets("Sheet1").ListObjects("Table1")
    Set projCols = Range(tbl & "[[initiative]:[milestone]]")
    Set projDict = CreateObject("Scripting.Dictionary")
    Set startCell = Worksheets("Sheet1").Range("D3") 'The first cell of the location where you want the list to be created.

    For Each r In projCols.Rows
        Call AddMilestoneToDictionary(projDict, r.Cells(1).Value, r.Cells(2).Value)
    Next

    Call writeInitiativeListToWorksheet(projDict, startCell)
End Sub

Private Sub AddMilestoneToDictionary(ByRef projDict, initiativeNumber As Variant, milestoneNumber As Variant)
    If projDict.Exists(initiativeNumber) Then
        projDict(initiativeNumber).Add milestoneNumber
    Else
        Set milestones = New Collection
        milestones.Add (milestoneNumber)
        projDict.Add initiativeNumber, milestones
    End If
End Sub

Private Sub writeInitiativeListToWorksheet(ByVal projDict, startCell As Range)
    Dim wrkSht As Worksheet
    Dim currentRow As Integer, initCol As Integer, mileCol As Integer

    Set wrkSht = startCell.Worksheet
    currentRow = startCell.Row
    initCol = startCell.Column
    mileCol = startCell.Column + 1

    For Each initiative In projDict.Keys
        wrkSht.Cells(currentRow, initCol).Value = initiative
        currentRow = currentRow + 1
        For Each milestone In projDict(initiative)
            wrkSht.Cells(currentRow, mileCol).Value = milestone
            currentRow = currentRow + 1
        Next
    Next
End Sub

使用透视表!谢谢,但我还是不想这样。。。然而,如果没有更好的解决方案,我将使用枢轴。非常感谢。为什么要避免使用透视表?这正是他们想要的。谢谢你的建议。我有一个甘特图,有15-20列的大量数据。所有的数据都是基于主动性或里程碑的。MarekRe我会在VBA中创建一个子例程或函数。我打赌有人能想出一种方法,只用工作表函数就可以做到这一点,但我的天哪,这可能会是一个大混乱。如果VBA没问题,那么我可以写一些东西给你。谢谢-但我希望单元格B3为空,第一个里程碑应该在B4中,等等。透视表不是完美的解决方案,因为我有一个甘特图连接到这个数据以及其他20列-它是一个表。好的,我想我无法帮助你,但希望其他人也能做到。您是否考虑过使用更灵活的工具(如R)创建此甘特图?