双击自动填充 - 基于相邻单元格的动态

时间:2012-06-29 12:44:35

标签: excel vba double-click

我需要这样做:

Excel Autofill

我在Excel中使用常规自动填充功能(双击单元格侧面的点)将内容复制到子单元格,因此在这种情况下单击单元格A1中的点将执行以下操作: / p>

enter image description here

我需要一个脚本,它将在整个列中重复该过程,直到相邻单元格中没有更多值。

2 个答案:

答案 0 :(得分:6)

您可以通过键盘执行此操作...

  • 选择A列
  • 选择主页|找到&选择|转到特别...空白确定
  • 按[=] [向上箭头] [ctrl + Enter]

要删除公式,请选择A列,复制和粘贴特殊值。

enter image description here

答案 1 :(得分:2)

大概这就是你要找的东西:

Option Explicit
Sub FillInTheBlanks()
    Dim StartCell As Range, EndCell As Range
    Set StartCell = ActiveCell
    Set EndCell = ActiveSheet.Cells(ActiveSheet.UsedRange.Rows.Count + 1, StartCell.Offset(0, 1).Column).End(xlUp)

    Dim currentText As String
    Dim i As Long
    For i = StartCell.Row To EndCell.Row
        If Not IsEmpty(ActiveSheet.Cells(i, StartCell.Row)) Then
            currentText = ActiveSheet.Cells(i, StartCell.Row).Text
        Else
            ActiveSheet.Cells(i, StartCell.Row).Value = currentText
        End If
    Next i
End Sub

该代码执行以下操作:

enter image description here


如果你真的想要截图中的内容,那么你需要这样做:

Option Explicit
Sub FillInTheBlanks()
    Dim StartCell As Range, EndCell As Range, NextCell As Range
    Set StartCell = ActiveCell
    Set EndCell = Cells(ActiveSheet.UsedRange.Rows.Count + 1, StartCell.Offset(0, 1).Column).End(xlUp)

    While StartCell.Row < EndCell.Row
        Set NextCell = StartCell.Offset(1, 1).End(xlDown).Offset(0, -1)
        StartCell.AutoFill Destination:=ActiveSheet.Range(StartCell, NextCell), Type:=xlFillDefault
        Set StartCell = NextCell.Offset(1, 0)
    Wend
End Sub

这是做什么的:

enter image description here