LoginSignup
5
8

More than 5 years have passed since last update.

Excelで選択したセルの合計値をクリップボードにコピーする

Last updated at Posted at 2018-03-23

選択したセルの合計値をクリップボードにコピーするExcelマクロです。
これでステータスバーに表示されている合計値を覚えておかなくても済みます。
個人用マクロブック(PERSONAL.XLSB)に保存し、"Ctrl+Shift+C"のようなショートカットキーを割り当てておくと便利です。
WorksheetFunction.Sum(Selection)Sumを書き換えれば、平均、個数、最小値、最大値などもコピーできます。

Excel.png

Sub 合計値をコピー()
'
' マクロ名: 合計値をコピー
' ショートカット キー: Ctrl+Shift+C
'

    If Not TypeOf Excel.Selection Is Excel.Range Then
        Call Excel.Selection.ShapeRange.PickUp 'PickUp = 本来のCtrl+Shift+Cの動作
        Exit Sub
    End If

    Dim mySelection As String
    mySelection = Application.WorksheetFunction.Sum(Selection)
    With CreateObject("Forms.TextBox.1")
        .MultiLine = True
        .Text = mySelection
        .SelStart = 0
        .SelLength = .TextLength
        .Copy
    End With

    ' 確認のポップアップが必要なら、コメントを外す
    ' Call VBA.MsgBox("選択されているセルの合計値をコピーしました" & vbLf & mySelection, , "選択値の合計")

End Sub

最近のWindows(64ビット版)ではDataObjectが不安定なため、代わりにTextBoxを利用。

5
8
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8