【问题标题】:How to format a multidimensionnal array depending on it's cell values?如何根据单元格值格式化多维数组?
【发布时间】:2015-04-15 16:47:17
【问题描述】:

我正在尝试在我的 Excel 工作表中格式化一个数组。对于我的表格中的每个元素及其编号x,我想在另一个表格中创建该元素的x 行,这有点与SUMIF 相反。 由于屏幕截图可能比长对话更明确,这就是我想要实现的目标:

如果有0 出现的水果,它不应该显示在GOALtable 中。

BaseGoal 表是硬写在我的 Excel 表中并作为示例,Current Result 表是用这个公式制作的:

法文版(我使用的是法文 Excel):

{=SIERREUR(
 INDEX(
  B$5:B$100;
  PETITE.VALEUR(
   SI(
    SI(
     ($C$5:$C$100>0);
     VRAI;
      FAUX
    );
    LIGNE(B$5:B$100)-LIGNE(B$5)+1
   );
   LIGNES(B$5:B5)
  )
 );
 ""
)} 

英文版(未测试):

{=IFERROR(
 INDEX(
  B$5:B$100;
  SMALL(
   IF(
    IF(
     ($C$5:$C$100>0);
     TRUE;
      FALSE
    );
    ROW(B$5:B$100)-ROW(B$5)+1
   );
   ROWS(B$5:B5)
  )
 );
 ""
)}

你能给我一些关于我如何实现这一目标的提示吗?我有什么方法可以在不使用 VBA 而只使用公式的情况下做到这一点?

注意: 我被要求在工作中这样做,我一生中从未使用过 Excel,对 VBA 一无所知。这就是为什么我对这项任务有点迷失。我通常用 Python 或 C++ 编写代码。我正在使用 Excel 2010。

【问题讨论】:

  • 看起来我在问这个问题之前所做的研究还不够好,这里有一个可能的duplicate。我会尝试基于此找到我自己的解决方案。
  • 好的。如果您在阅读该帖子后仍决定想要一个纯粹基于公式的解决方案,请告诉我。
  • 我设法使用 VBA 完成了我想做的事情,感谢您的宝贵时间。
  • 好的,感谢您回复我,很高兴您找到了解决方案。

标签: excel excel-formula excel-2010


【解决方案1】:

我设法使用 VBA 实现了我的目标,这是我使用的代码:

Public Sub GenerateTable()
    ' This routing will copy rows based on the quantity to a new sheet.
    Dim rngSinglecell As Range
    Dim rngQuantityCells As Range
    Dim intCount As Integer
    Dim countCell As Integer

    ' Set this for the range where the Quantity column exists. This works only if there are no empty cells
    Set rngQuantityCells = Sheets("Feuil3").Range("C5", Sheets("Feuil3").Range("C5").End(xlDown))

    countCell = 4
    For Each rngSinglecell In rngQuantityCells
        ' Check if this cell actually contains a number
        If IsNumeric(rngSinglecell.Value) Then
            ' Check if the number is greater than 0
            If rngSinglecell.Value > 0 Then
                ' Copy this row as many times as .value
                For intCount = 1 To rngSinglecell.Value
                    ' Copy the row into the next emtpy row in sheet2
                    countCell = countCell + 1
                    Intersect(Range(rngSinglecell.Address).EntireRow, Columns("B:D")).Copy Destination:=Sheets("Feuil3").Range("G" & CStr(countCell))
                Next
            End If
        End If
    Next
End Sub

这是输出:

基于 Matt 在duplicate 中的回答的解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2013-06-10
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多