1

我正在尝试使用 Excel 填充我拥有的组织层次结构中的空白单元格。之前和之后的场景如下。

我已经尝试过使用伪代码。

前

后

For all rows
For all cells in row
If cell is populated, row++
If cell is not populated, copy the value immediately above to the current cell and check next cell in row.
  1. 这个逻辑合理吗?
  2. 我将如何在 VBA 中实现这一点?

我猜我可以像这样遍历每一行中的每个单元格......

For Each row In rng.Rows
For Each cell in row.Cells
'Do Something
Next cell
Next row

但后来我卡住了!任何帮助将不胜感激。

4

1 回答 1

1

你的逻辑是合理的。尝试这样的事情:

Sub FillTree()
Dim rng as Range
Dim r as Range
Dim c as Range
Dim cl as Range

Set rng = Range("A1:E50") '## Modify this to the full range you want to fill in.

For Each r In rng.Rows                     '# iterate over each column
    For Each cl in r.Cells                 '# check each cell in the column
        If Trim(cl) = vbNullString Then    '# if the cell is empty, fill from above
            cl = cl.Offset(-1,0)
        Else:                              '# skip to the next row if populated
            Exit For
        End If
    Next
Next 

End Sub
于 2013-07-18T16:19:39.283 回答