自动增量,无需鼠标

阿尔特姆·鲍里索夫(Artem.Borysov)

我在单元格1中书写,然后将鼠标放在该单元格右下角的黑色方块上,右键单击并拖动。

主要问题-我不想使用鼠标。并查找脚本或键盘热键以获得相似的数字

1 3

3 6

5 9

从选择的关键字

1 3

3 6

迪克·库斯莱卡(Dick Kusleika)

我将以下宏放入“个人宏工作簿”中,并分配了键盘快捷键。

Sub FillSeries()

    Dim lFirstBlank As Long

    If TypeName(Selection) = "Range" Then
        If Selection.Columns.Count = 1 Or Selection.Rows.Count = 1 Then
            lFirstBlank = GetFirstBlank(Selection)
            If lFirstBlank = 0 Then
                SelectAdjacentCol
                lFirstBlank = GetFirstBlank(Selection)
            End If
            If lFirstBlank > 1 Then
                If Selection.Columns.Count = 1 Then
                    Selection.Cells(1).Resize(lFirstBlank - 1).AutoFill _
                        Selection, xlFillSeries
                ElseIf Selection.Rows.Count = 1 Then
                    Selection.Cells(1).Resize(, lFirstBlank - 1).AutoFill _
                        Selection, xlFillSeries
                End If
            End If
        End If
    End If

End Sub

Function GetFirstBlank(rRng As Range) As Long

    Dim i As Long

    i = 0

    For i = 1 To rRng.Cells.Count
        If IsEmpty(rRng.Cells(i)) Then
            GetFirstBlank = i
            Exit For
        End If
    Next i

End Function

Sub SelectAdjacentCol()

    Dim rAdjacent As Range

    If TypeName(Selection) = "Range" Then
        If Selection.Column > 1 Then
            If Not IsEmpty(Selection.Offset(0, -1).Value) Then
                With Selection.Offset(0, -1)
                    Set rAdjacent = .Parent.Range(.Cells(1), .End(xlDown))
                End With

                Selection.Resize(rAdjacent.Cells.Count).Select
            End If
        End If
    End If

End Sub

另请参见http://dailydoseofexcel.com/archives/2008/07/17/fillseries-keyboard-shortcut/

更新

如果只想填充列,并且要填充选择中的所有列,则下面的代码应该可以完成您想要的操作。它还会查看列中最后一个单元格的NumberFormat,并将填充单元格的NumberFormat改回到该格式选择最后一个单元格有点武断,但这就是事实。

Sub FillSeriesForAllColumns()

    Dim lFirstBlank As Long
    Dim rCol As Range
    Dim sOldNumberFormat As String

    If TypeName(Selection) = "Range" Then
        For Each rCol In Selection.Columns
            sOldNumberFormat = Selection.Cells(Selection.Cells.Count).NumberFormat
            lFirstBlank = GetFirstBlank(rCol)
            If lFirstBlank = 0 Then
                SelectAdjacentCol
                lFirstBlank = GetFirstBlank(rCol)
            End If
            If lFirstBlank > 1 Then
                rCol.Cells(1).Resize(lFirstBlank - 1).AutoFill _
                    rCol, xlFillSeries
            End If

            rCol.Offset(lFirstBlank - 1, 0).Resize(rCol.Row - (lFirstBlank - 1)).NumberFormat = sOldNumberFormat

        Next rCol
    End If

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章