2
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.

VBA 便利コードサンプル

Last updated at Posted at 2023-03-05

VBAで保存ショートボタンが押下された時に、全てのシートのカーソルをA1セルに移動させた後、アクティブシートに戻るコード

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim currentSheet As Worksheet
    
    '現在のアクティブシートを保存
    Set currentSheet = ActiveSheet
    
    'フォント種別を変更する
    ChangeFont
    
    'カーソルを移動する
    MoveCursor
    
    'アクティブシートに戻す
    currentSheet.Activate
End Sub
'フォント種別を変更する
Sub ChangeFont()
    Dim ws As Worksheet
    Dim cell As Range
    
    For Each ws In ActiveWorkbook.Worksheets
        For Each cell In ws.UsedRange.Cells
            cell.Font.Name = "MS Pゴシック"
        Next cell
    Next ws
End Sub
'カーソルを移動する
Sub MoveCursor()
    Dim ws As Worksheet
    
    For Each ws In ActiveWorkbook.Worksheets
        ws.Activate
        ws.Range("A1").Select
    Next ws
End Sub

2
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
2
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?