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?

More than 1 year has passed since last update.

AccessのVBAでエクセルのセル入力規則(ドロップダウン)を設定

Last updated at Posted at 2023-05-18

概要

これをVBAで動的に設定したい。
image.png

やり方

参照設定に『Microsoft Excel 16.0 Object Library』を追加。
image.png

サンプル
Dim excel_ As New Excel.Application
Dim excel_sheet_ As Excel.Worksheet

'バックグラウンドで起動。
excel_.Visible = False
excel_.UserControl = False

'Accessファイルと同じフォルダーにある『tes.xlsx』が対象。
excel_.Workbooks.Open FileName:=CurrentProject.Path & "\tes.xlsx"

'一番左のシートのみを選択状態に。
Set excel_sheet_ = excel_.Worksheets(1)
excel_sheet_.Select Replace:=True

'1行3列目(C1セル)に対して設定。

'選択した時に『1.88E+08』のようにならないよう、セルは文字列にしておいた方がいい。
excel_sheet_.Cells(1, 3).NumberFormatLocal = "@"

'既に入力規則が設定されていた場合、それが悪影響しないように一旦削除をかませる。
excel_sheet_.Cells(1, 3).Validation.Delete

'入力規則設定。
excel_sheet_.Cells(1, 3).Validation.Add Type:=xlValidateList, Operator:=xlEqual, Formula1:="あ,い,う"

'変更保存してエクセルファイル閉じる。
excel_.Workbooks(1).Close SaveChanges:=True
excel_.Quit

実行結果は下記。
image.png

参考サイトさん

バージョン

Windows 10 Pro 22H2 19045.2965
Microsoft Access for Microsoft 365 MSO (バージョン 2304 ビルド 16.0.16327.20200) 32 ビット
Microsoft Excel for Microsoft 365 MSO (バージョン 2304 ビルド 16.0.16327.20200) 32 ビット

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?