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?

【VBA入門⑲】現場で本当にあった!社内業務の要望とVBAでの解決例📋✨

Posted at

第19章:社内業務でよくある要望と対処例

こんにちは、まくるだよ🐰📋
今回は、実際の職場でよく聞く 「こんなことできない?」 に対して、
VBAでどう対応できるかの魔法のヒント集をお届けっ✨


✅ よくある要望①

「複数のExcelファイルからデータを集めて一覧にできない?」

🔮 解決マクロ(指定フォルダ内をループ)

Sub CollectFromFolder()
    Dim fso As Object, folder As Object, file As Object
    Dim wb As Workbook, ws As Worksheet
    Dim path As String
    Dim destRow As Long

    path = "C:\Users\user\Desktop\報告書\"
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(path)
    destRow = 2

    For Each file In folder.Files
        If InStr(file.Name, ".xlsx") > 0 Then
            Set wb = Workbooks.Open(file.Path)
            Set ws = wb.Sheets(1)
            ThisWorkbook.Sheets("集計").Cells(destRow, 1).Value = ws.Range("A1").Value
            destRow = destRow + 1
            wb.Close False
        End If
    Next
End Sub

✅ よくある要望②

「入力ミスを防ぐためにリストから選べるようにしてほしい」

🔮 解決マクロ(入力規則の設定)

Sub SetValidationList()
    With Range("B2:B100").Validation
        .Delete
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
            Formula1:="営業部,総務部,経理部"
        .IgnoreBlank = True
        .InCellDropdown = True
    End With
End Sub

📌 入力時にプルダウンリストが表示されるよ!


✅ よくある要望③

「印刷するとき、指定範囲だけA4に収めてほしい!」

🔮 解決マクロ(印刷範囲+縮小設定)

Sub SetPrintSettings()
    With ActiveSheet.PageSetup
        .PrintArea = "A1:F30"
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    ActiveSheet.PrintPreview
End Sub

✅ よくある要望④

「名前の一覧に“様”をつけたいんだけど…」

🔮 解決マクロ(一括加工)

Sub AddSama()
    Dim cell As Range
    For Each cell In Range("A2:A100")
        If cell.Value <> "" And Right(cell.Value, 1) <> "様" Then
            cell.Value = cell.Value & "様"
        End If
    Next
End Sub

📌 「すでに様付き」は無視して2重にならないようにしてるよ✨


✅ よくある要望⑤

「入力日付は自動で今日の日付が入るようにしたい」

🔮 解決マクロ(セル変更時に自動記入)

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("B2:B100")) Is Nothing Then
        Application.EnableEvents = False
        Target.Offset(0, 1).Value = Date
        Application.EnableEvents = True
    End If
End Sub

📌 Worksheet_Change イベントを使えば、セル変更時に自動記入できる!


📌 まとめ

  • 実務の「ちょっとした手間」をVBAで解決すると大喜びされる!
  • 「同じ作業を毎日やってる」→ 自動化チャンス! ** - 「入力ミスを防ぎたい」→ ** ルール付けや自動記入で補助!
  • 要望の裏には“改善ポイント”が必ずあるよ✨

次回は、 「第20章:VBAスキルを次のステージへ」
まくると一緒に、脱・初心者のその先へレベルアップしよう🐰🌟

VBA初心者 #Excelマクロ #業務改善 #社内効率化 #実践例

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?