自定義 Excel 自動填充快捷鍵


 

Microsoft Excel 提供了豐富的快捷鍵,使得操作電子表格十分方便,尤其是在操作很大,很長的表時。

Excel的自動填充功能,使得復制單元格內容、公式和填充序列的操作變得簡單。但是,Excel只提供了通過拖動填充柄來實現填充,並沒有為自動填充提供快捷鍵。在操作十分頻繁的時候,右手必須在鍵盤和鼠標之間來回移動,容易疲勞。系統不提供,作為程序員,可以自己編寫一個VBA宏執行自動填充,並為該宏指定快捷鍵,從而實現通過快捷鍵執行自動填充操作。

 

1: 新建一個Excel工作簿;在VBA編輯器中添加一個模塊,編寫宏代碼。

源碼:

' Author: Ken Yang
' http://www.cnblogs.com/kenyang
' 使用和分發,請保留完整作者信息。

Sub AutoFill()

    Dim SourceArea As Range, TargetArea As Range
    
    Set SourceArea = Selection.Areas.Item(1)
    
    Dim SourceBottomLeftCell As Range, SourceBottomRightCell As Range
    
    Set SourceBottomLeftCell = SourceArea.Cells(SourceArea.Rows.Count, 1)
    Set SourceBottomRightCell = SourceArea.Cells(SourceArea.Rows.Count, SourceArea.Columns.Count)
    
    Dim TargetLastRow As Long, TargetLastCell As Range
    
    If SourceBottomLeftCell.Offset(1, 0).Value = vbEmpty Then
    
        If SourceBottomLeftCell.End(xlDown).Value = vbEmpty Then
            If SourceBottomLeftCell.Column = 1 Then
                If SourceBottomRightCell.Offset(1, 1).Value <> vbEmpty Then
                    Set TargetLastCell = SourceBottomRightCell.Offset(0, 1).End(xlDown).Offset(0, -1)
                End If
            Else
                If SourceBottomLeftCell.Offset(1, -1).Value <> vbEmpty Then
                    Set TargetLastCell = SourceBottomLeftCell.Offset(0, -1).End(xlDown).Offset(0, SourceArea.Columns.Count)
                End If
            
            End If
        Else
            Set TargetLastCell = ActiveSheet.Cells(SourceBottomLeftCell.End(xlDown).Offset(-1, 0).Row, SourceBottomRightCell.Column)
        End If
    
    Else 'If SourceBottomLeftCell.Offset(1, 0).Value <> vbEmpty Then
    
        Dim LastCell As Range
        Set LastCell = SourceBottomLeftCell.End(xlDown)
    
        Dim i As Long, LastRow As Long
        LastRow = SourceBottomLeftCell.End(xlDown).Row
        
        For i = 2 To SourceArea.Columns.Count
            If SourceArea.Cells(SourceArea.Rows.Count, i).Offset(1, 0).Value = vbEmpty Then
                LastRow = SourceBottomLeftCell.Row
                Exit For
            Else
                Dim CurrentLastCell As Range
                Set CurrentLastCell = SourceArea.Cells(SourceArea.Rows.Count, i).End(xlDown)
                If CurrentLastCell.Row < LastCell.Row Then
                    LastRow = CurrentLastCell.Row
                End If
            End If
        Next i
        
        Set TargetLastCell = ActiveSheet.Cells(LastRow, SourceBottomRightCell.Column)
    
    End If
    
    If Not TargetLastCell Is Nothing Then
    
        Dim TargetAreaName As String
        
        TargetAreaName = SourceArea.Cells(1, 1).Address(0, 0) & ":" & TargetLastCell.Address(0, 0)
        Set TargetArea = Range(TargetAreaName)
        
        On Error GoTo ErrHandler
        
        SourceArea.AutoFill Destination:=TargetArea
    
        TargetArea.Select
    
    End If

ErrHandler:

End Sub

 

2: 為宏指定快捷鍵 Ctrl + E。 使用Ctrl + E 作為快捷鍵的原因是 Ctrl + E 沒有被Excel內置功能占用,並且Ctrl 鍵和 E 鍵都在鍵盤的左邊區域,左手單手就可以操作。

3: 將工作簿另存為 Excel 加載宏。

4: 將Excel加載宏 復制到 %programfiles%\Microsoft Office\Office12\XLSTART 目錄(Excel 2003 目錄:%programfiles%\Microsoft Office\Office11\XLSTART),以便Excel啟動時,自動加載宏代碼。

 

也可從這里下載宏文件,解壓后直接放置到%programfiles%\Microsoft Office\Office12\XLSTART 目錄,即可使用。下載的宏文件只支持Excel 2007。如果是Excel 2003, 請按照前面的步驟操作。

 

這樣,可以在Excel 中按下 Ctrl + E 實現向下自動填充了。

 

缺陷:

VBA宏代碼執行的操作,不支持撤銷(undo)。

 

報告bug,請留評論。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM