0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【備忘録】VBAからPower Queryパラメータを動的に更新する最小構成

0
Last updated at Posted at 2025-11-24

〜Excel側から値だけ差し替えてクエリを再計算させる仕組み〜

概要

Excel VBAから Power Query のパラメータ(M言語)を直接書き換え、
外部入力した値をそのままクエリ処理に反映させるための最小構成の備忘録メモ。

用途:年月・ID・検索キーなど、Power Query側を開かずに外部から値を差し替えたいとき


Power Query:パラメータ初期フレーム

Power Query で 空のクエリを作成 → 名前を YM に変更 → 下記を貼り付け
これは「初期値」で、実際の処理では後から VBA が上書きする。

let
    value = 202501
in
    value

VBA:パラメータを書き換えて再計算

ユーザー入力で受け取った値を、そのまま Power Query パラメータ YM に上書き。
Power Query を開かなくても値が差し替わる。

Sub UpdateYM()
    Dim ym As Long
    ym = CLng(InputBox("YMを入力(例:202510)"))
    
    ' パラメータクエリ "YM" のM言語を直接書き換え
    ThisWorkbook.Queries("YM").Formula = _
        "let value = " & ym & " in value"
    
    ' クエリ再計算
    ThisWorkbook.RefreshAll
End Sub

シチュエーション

タイミング

・ワークシート起動時
・ユーザー操作時

内容

・フォルダパス
・Config設定情報
・ユーザーが入力する対象年月(シート名)
など

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?