【问题标题】:Fill empty cells between two filled cells填充两个填充单元格之间的空单元格
【发布时间】:2017-01-10 13:56:43
【问题描述】:

情况:
在单元格“A1”上,我的值为“1”
在单元格“A10”上,我的值为“2”
在单元格“A20”上,我的值为“3”
在单元格“A30”上,我的值为“4”

我想用 Excel VBA 做什么:
A1 和 A10 之间有空单元格。我希望 A2:A9 用 A10 的值填充,即“2”。
A10 和 A20 之间有空单元格。我希望 A11:19 用 A20 的值填充,即“3”。

问题是,A1 到 A30 的范围不固定。我想在整行中搜索不为空的单元格,并用已填充的上部单元格填充它们之间的单元格。

编辑:
为了解释更多,我有一个 Access 数据库,其中包含一个填充日期的表和一个填充数字的表。

我想向 Excel 表格制作报告。

Dim Daten As Variant
Daten = Array(rs!DatumJMinus8Monate, rs!DatumJ, rs!DatumI, rs!DatumH, rs!DatumG, rs!DatumF, rs!DatumE, rs!DatumD, rs!DatumC, rs!DatumB, rs!DatumA, rs!DatumA4Monate)
Dim Bedarfe As Variant
Bedarfe = Array(rs!BedarfJ8Monate, rs!BedarfJ, rs!BedarfI, rs!BedarfH, rs!BedarfG, rs!BedarfF, rs!Bedarfe, rs!BedarfD, rs!BedarfC, rs!BedarfB, rs!BedarfA, rs!BedarfA, "")

Dim neuereintrag As Boolean
bedarfindex = 0
For Each element In Daten
    i = 7
    For jahre = 1 To 10
        If Cells(1, i + 1) = Year(element) Then
            For monate = 1 To 12
                If Cells(2, i + monate) = Month(element) Then
                    Cells(zeile, i + monate) = Bedarfe(bedarfindex)
                    Cells(zeile, i + monate).Font.Bold = True
                    bedarfindex = bedarfindex + 1
                    neuereintrag = True
                ElseIf IsEmpty(Cells(zeile, i + monate)) Or neuereintrag = True Then
                    Cells(zeile, i + monate) = Bedarfe(bedarfindex)
                    neuereintrag = False
                End If
            Next monate
        End If
        i = i + 12
    Next jahre
Next element

图中红圈内的数字必须删除。

【问题讨论】:

  • 你说过你尝试了很多方法,你能展示一些你做了什么吗?这不是“我的代码”网站,但如果您给我们一些帮助,我们将帮助您实现目标!
  • 编辑您的问题并在其中添加您的code
  • 我会在几分钟内完成,需要一些时间
  • 代码是德文的,你要我把变量名改成英文吗?
  • On the Cell "A30" i have the value "4" - 这个值会被复制到第 1048576 / 65536 行吗?

标签: vba excel cells


【解决方案1】:

从下往上上班:

Sub FillUp()
    Dim N As Long
    Dim i As Long
    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = N - 1 To 1 Step -1
        If Cells(i, 1).Value = "" Then Cells(i, 1).Value = Cells(i + 1, 1).Value
    Next i
End Sub

【讨论】:

  • 嘿,真的很好用!谢谢!唯一的问题是,我在描述上犯了一个错误。我想填充 A1、H1、O1 等之间的单元格。不是一列,而是一行。我会尝试更改您的代码,但如果您能告诉我我必须更改的内容也适用于行,我也会感谢您?
  • 谢谢你!我做到了!你是我的英雄!这是代码:Sub FillUp() Dim N As Long Dim i As Long N = Cells(1, Columns.Count).End(xlToLeft).Column For i = N - 1 To 1 Step -1 If Cells(1, i).Value = "" Then Cells(1, i).Value = Cells(1, i + 1).Value Next i End Sub
  • @oemerkk 干得好!!
【解决方案2】:

也许是这样的。它需要一些工作,因为如果两个值彼此相邻,或者第 1 列不包含值,它将失败。

Sub AutoFill()

    Dim rCell1 As Range, rCell2 As Range

    With ThisWorkbook.Worksheets("Sheet1")
        'Find the last column containing data, set both references to this
        'so the Do While doesn't fall over on the first loop.
        Set rCell2 = .Cells(1, .Columns.Count).End(xlToLeft) '1 is the row number it's looking at.
        Set rCell1 = rCell2

        'Find next cell to the left containing data and fill between these two columns.
        Do While rCell1.Column <> 1
            Set rCell1 = rCell2.End(xlToLeft)
            .Range(rCell1, rCell2.Offset(, -1)).FillRight
            Set rCell2 = rCell1
        Loop
    End With

End Sub

【讨论】: