我不太精通 VBA,似乎不知道如何写这个。
我有一组值,其中有空白行,需要与下一行数据相同。
如上所示,我希望第 1-5 行与第 6 行相同。 这些空白确实会在集合中重复出现,并且也需要以类似方式填补。
我一直在尝试使用 if isempty 复制下面的行来创建一些东西,但不确定如何以相反的顺序进行 for 循环。
Sub FillRowsFromBelow()
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim rg As Range:
With ws.UsedRange
Set rg = ws.Range("A2", .Cells(.Cells.CountLarge))
End With
Dim rCount As Long: rCount = rg.Rows.Count
Dim cCount As Long: cCount = rg.Columns.Count
Dim rrg As Range, srrg As Range, drrg As Range, r As Long
Dim CopyRows As Boolean, IsBlankRowFound As Boolean
For r = rCount - 1 To 1 Step -1
Set rrg = rg.Rows(r)
If Application.CountBlank(rrg) = cCount Then ' row is blank
If IsBlankRowFound Then ' all but bottom blank row
Set drrg = Union(drrg, rrg)
Else ' bottom blank row
Set srrg = rrg.Offset(1)
Set drrg = rrg
IsBlankRowFound = True
End If
If r = 1 Then CopyRows = True
Else ' row is not blank
If IsBlankRowFound Then
IsBlankRowFound = False
CopyRows = True
End If
End If
If CopyRows Then
' Copy values, formulas and formats.
srrg.Copy drrg
' Or copy only values (faster).
'drrg.Value = srrg.Value
End If
Next r
MsgBox "Blank rows filled from below.", vbInformation
End Sub