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でエクセルシートの列幅を自動調整

Posted at

概要

これを…
image.png

こうしたい。
image.png

一つ一つの列幅をいちいち指定したくない。
全選択 ⇒ 境界線をダブルクリック のように自動調整したい。
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

'列幅自動調整。
excel_sheet_.Cells.Columns.AutoFit

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

バージョン

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?