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.

【Excel VBA】A1保存

Posted at

目的

以下の条件を達成したい

  • すべてのシートでA1セルが選択される
  • 一番前のシートに移動
  • 上書き保存をしてExcelを閉じる

コード

A1セルを選択する関数と、上書き保存して閉じる関数を作成する。
作成した関数をそれぞれ呼び出す。

A1保存関数

Function A1保存()
    Dim sht As Worksheet '処理中のワークシート
    Dim shtVisible '表示可能なワークシート
    Dim iRow, iCol '縦横座標
    Dim oFilterStatus As AutoFilter 'オートフィルタ状態
    Dim oRngeFilter As Range 'オートフィルタ設定

    For Each sht In Sheets
        If (IsEmpty(shtVisible) = true) And (sht.Visible <> xlSheetHidden) And (sht.Visible <> xlSheetVeryHidden) then
            Set shtVisible = sht
        End If

        'シートが表示されている場合
        If (sht.Visible <> xlSheetHidden) And (sht.visible <> xlsheetVeryHidden) Then
            sht.Select
            
            'ウィンドウ枠の固定がされている場合
            If ActiveWindow.FreezePanes = True Then
                iRow = Active.Window.SplitRow + 1
                iCol = Active.Window.SplitColumn + 1
                Cells(iRow + 1, iClo + 1).Active
            End If

            Set oFilterStatus = sht.AutoFilter
            'オートフィルタが設定されている場合
            If Not oFilterStatus Is Nothing Then
                'フィルタがかかっている場合
                If oFilterStatus.FilterMode = True Then
                    'フィルタがかかっている行の先頭を選択
                    Set oRngeFilter = Range("A1").CurrentRegion
                    Set oRngeFilter = Application.Intersect(oRngeFilter, oRngeFilter.Offset(1, 0))
                    Set oRngeFilter = oRngeFilter.SpecialCells(xlCellTypeVisible)
                    Range("A" & CStr(oRngeFilter.row)).Select
                End If
            End If

            sht.Range("A1").Select
        End If
    Next

    shtVisible.Select
End Function

上書き保存からの閉じる関数

Function 上書き保存からの閉じる()
    Application.DisplayAlerts = False
    ActiveWorkBook.Save
    ActiveWorkBook.Close
    Application.DisplayAlerts = True
End Function

A1上書き保存

上記二つの関数を呼び出す。
個人用マクロブックに作成して、ショートカットを設定するなどすると使いやすいです。

Sub A1上書き保存()
    Call A1保存
    Call 上書き保存からの閉じる
End Sub
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?