最近、PowerQueryづいている私ですが、VBAとPowerQueryを組み合わせるとより効果が発揮できるというか、PowerQuery単体ではどうしてもデータを集計するしかできなくて、人間が操作すりゃいいのですが、それも面倒な時があるので、セルを対象となるクリックしておけば一発で呼び出せる機能をマクロ化してみました。
まさにこれがマクロという使い方ですね。
VBAコードを載せますのでコピーしてお使いください。
テーブル領域以外を選択してたら何も起きないようにしておきました。
Sub テーブル更新()
If テーブル領域判定() Then
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End If
End Sub
Sub テーブル削除()
If テーブル領域判定() Then
If Selection.ListObject.ListRows.Count >= 1 Then
Selection.ListObject.DataBodyRange.Delete
End If
End If
End Sub
Sub テーブルコピー()
If テーブル領域判定() Then
If Selection.ListObject.ListRows.Count >= 1 Then
Selection.ListObject.DataBodyRange.Copy
End If
End If
End Sub
Sub テーブル貼り付け()
If テーブル領域判定() Then
Selection.ListObject.Range.Cells(2, 1).PasteSpecial (xlPasteValues)
End If
End Sub
Sub テーブル行追加()
If テーブル領域判定() Then
Selection.ListObject.ListRows.Add AlwaysInsert:=True
End If
End Sub
Sub テーブル行削除()
Dim r As Long
If テーブル領域判定() Then
If Selection.ListObject.ListRows.Count >= 1 Then
r = Selection.Row - Selection.ListObject.Range.Cells(1, 1).Row
Selection.ListObject.ListRows(r).Delete
End If
End If
End Sub
Sub テーブルすべて更新()
If テーブル領域判定() Then
ActiveWorkbook.RefreshAll
End If
End Sub
Function テーブル領域判定()
テーブル領域判定 = Not (Selection.ListObject Is Nothing)
End Function


コメント