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 に答える