Excel公式根据其他variables空出范围

对 – 这是一个棘手的词组,所以我要用几个图像来帮助我。

在列A和B中是各个队名的变化列表和每个队的队员人数。

D列包含所需的输出。

我需要一个公式,将其插入到单元格D2中,并将其拖至列B的总数中,以返回团队名称 – 但是至关重要的是允许多行下面的行返回空白。 下面的空白行数实际上等于1 – 该组中的玩家数量。

Excel公式根据其他变量空出范围

我已经给了它一些想法,但不能拿出一个合适的公式。 有任何想法吗?

也欢迎build议更好的标题。

下面的VBA函数将完成你想要的。 让我知道,如果它的任何部分不清楚给你。

Sub teamRows() Dim colDRowNumber As Integer Dim i As Integer Dim teamName As String Dim numberOfRows As Integer Dim HowFar As Integer ' Loop through the teams in column A HowFar = Application.WorksheetFunction.CountA(Range("A:A")) ' Variable to keep count of rows in column D colDRowNumber = 2 For i = 2 To HowFar ' Get the team's name and number of rows teamName = Range("A" & i).Value numberOfRows = Range("B" & i).Value ' Fill in the team's name in column D Range("D" & colDRowNumber).Value = teamName ' Increase the row number by the number of empty rows required colDRowNumber = colDRowNumber + numberOfRows Next i End Sub 

一个复杂但短暂的尝试 – 我想避免循环。

以下示例适用于A2至A20

 y = Split(Join(Application.Transpose(Application.Evaluate("=index(substitute(substitute(substitute(REPT(A2:A20 &"","",B2:B20),A2:A20&"","",""X"",1),A2:A20,""""),""X"",A2:a20),0,1)")), ","), ",") [d2].Resize(UBound(y)) = Application.Transpose(y)