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でエクセルファイル内の特定名シートを左端or右端に移動

Last updated at Posted at 2023-05-12

やり方

参照設定で下記を追加。
image.png

サンプル
'フォーム上に『btn_1』という名前のオブジェクトがある前提。
'そのクリックイベント。
Private Sub btn_1_Click()

    Dim excel_ As New Excel.Application
    
    'バックグラウンドで起動
    excel_.Visible = False
    excel_.UserControl = False
        
    'Accessファイルと同じフォルダーにあるtest.xlsxが対象。
    excel_.Workbooks.Open FileName:=CurrentProject.Path & "\test.xlsx"
    
    '『あ』という名前のシートが無いと『インデックスが有効範囲にありません』の例外になるので注意。
    
    '一番左に移動。
    '『あ』という名前のシートが既に一番左になっていたとしても、例外にはならない。
    excel_.Worksheets("あ").Move Before:=excel_.Worksheets(1)
    
    '一番右に移動。
    '『あ』という名前のシートが既に一番右になっていたとしても、例外にはならない。
    'excel_.Worksheets("あ").Move After:=excel_.Worksheets(excel_.Worksheets.Count)
    
    '保存して閉じる。
    excel_.Workbooks(1).Close SaveChanges:=True
    excel_.Quit
    
End Sub

参考サイトさん

バージョン

Windows 10 Pro 22H2 19045.2965
Microsoft Access 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?