1
2

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 5 years have passed since last update.

現在アクティブなワークブックの選択セルをA1に揃えるVBAマクロ

Last updated at Posted at 2019-03-21

Excelを保存して閉じる際、A1セルが選択された状態にせず怒られたことがあります。
そんなわけで、すべてのシートの選択セルをA1にするVBAマクロです。
過去にも同様のテーマの記事がありますが、非表示・保護シートのスキップや固定ウィンドウのスクロールも戻すなど、豪華版となっております。

環境

  • Windows7 64bit
  • Excel 2013
  • VBA 7.1

所有するWin10にはMSOfficeが入っていないため、確認したのはWin7です。
でもWin10でもおそらくそのまま動くと思います。

ExcelVBAソース

Sub selectA1()
  Dim shItem As Worksheet
  Dim firstIndex As Long
  Dim msg As String
   
  Application.ScreenUpdating = False
  
  For Each shItem In ActiveWorkbook.Worksheets
    ' 非表示・セル選択禁止シートはスキップ
    If (shItem.visible = xlSheetVisible) And (shItem.EnableSelection = xlNoRestrictions) Then
      Application.Goto Reference:=shItem.Cells(1, 1), Scroll:=True
      If firstIndex = 0 Then
        firstIndex = shItem.Index
      End If
    Else
      If shItem.visible <> xlSheetVeryHidden Then
        msg = msg & vbCrLf & shItem.Name
      End If
    End If
  Next shItem
  
  With ActiveWorkbook
    .Worksheets(firstIndex).Select
    Application.ScreenUpdating = True
    
    If msg <> "" Then
      msg = vbCrLf & vbCrLf & "以下のシートには適用されませんでした。" & msg
    End If
    
    If vbYes = MsgBox("完了しました。" & vbCrLf & "上書き保存して終了しますか?" & msg, vbYesNo) Then
      .Save
      .Close
    End If
  End With
End Sub

利用方法

個人用マクロブックの表示とマクロの登録方法
単一のブックにすべてのマクロを作成して保存する | MSOffice Support
マクロへのショートカット登録などの方法
マクロを実行する | MSOffice Support

1
2
2

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?