在两个填充的单元格之间填充空单元格
情况:
在单元格“A1”我有值“1”
在单元格“A10”我有值“2”
在单元格“A20”我有值“3”
在单元格“A30”我有值“4”
我想用Excel VBA做什么:
A1和A10之间有空单元格。 我想要A2:A9填充A10的值,即“2”。
在A10和A20之间有空单元格。 我想让A11:19填上A20的值,意思是“3”。
问题是,范围A1到A30是不固定的。 我想search整行不是空的单元格,并填充它们之间的单元格与填充的上单元格。
编辑:
为了更多的解释,我有一个Access数据库,里面充满了date和一个充满数字的表格。
我想做一个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
在图片中,红圈中的数字必须被删除。
在从上到下的工作方式上:
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
也许这样的事情 它需要一些工作,因为如果两个值彼此相邻,或者列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