1
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?

More than 3 years have passed since last update.

ExcelVBAのSQL:結合した結果で更新(Join & Update)

Last updated at Posted at 2021-06-10

1.実行条件

  • Excelのバージョン:MS365 64bit
  • VBAのExcel参照ライブラリ:Excel 16 Object Library
  • DBプロバイダー:Microsoft.ACE.OLEDB.12.0, Excel 12.0

2.サンプルデータの構造(同様の構造のシートが2つ存在)

image.png

3.2つのシートを結合して、一方のシートの値で別のシートの値を更新(Join & Update)

  • 以下、SQLのみ記載。
  • Joinする際に対象シートに抽出条件を設定。
UPDATE 
  (SELECT * FROM [シート1$] WHERE CStr([店舗ID]) <> '' AND [項目タイプ] = 'EX') AS T_1
  LEFT JOIN [シート2$] AS T_2 
  ON T_1.[会社ID] = T_2.[会社ID]   
    AND T_1.[店舗ID] = T_2.[店舗ID]   
    AND T_2.[項目タイプ] = T_2.[項目タイプ] 
SET T_2.[入金] = T_1.[売上] 

4.エラー対処

  • 文字列の条件は、ダブルクオーテーションではなくシングルにしないと以下のエラーメッセージが表示される。(<< X >>エラー箇所数)
[Microsoft][ODBC Excel Driver]パラメーターが少なすぎます。<< X >>を指定してください。
1
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
1
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?